Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Sort Array Array()

Author
Message
Battoad
AGK Developer
19
Years of Service
User Offline
Joined: 12th Feb 2007
Location: A Dark Place
Posted: 28th May 2011 19:26
Ocassionally i need to sort arrays, hi to lo and normally this would be achieved by comparing and swapping values.
I have found a "sort array" command in IanM's utilities but am unable to work out how this works.
If anyone has used this is there any chance you could post a short example of how it should be used. (a non-udt array should suffice to get me going with it, after which i should be able to work out the udt array version).
Thanks.

Battoad
AGK Developer
19
Years of Service
User Offline
Joined: 12th Feb 2007
Location: A Dark Place
Posted: 28th May 2011 19:38
sorry folks, sorted it - pun not intended.



very basic but the basics of it working.

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 28th May 2011 23:58
UDT arrays are almost as simple to sort - what you do is specify the UDT field numbers after the array:



Battoad
AGK Developer
19
Years of Service
User Offline
Joined: 12th Feb 2007
Location: A Dark Place
Posted: 29th May 2011 12:11
Thanks IanM, that additional info has cleared up points that I was trying to achieve. Its actually quite brilliant, thanks again.

toggy
21
Years of Service
User Offline
Joined: 6th Jun 2005
Location: Serbia
Posted: 11th Jul 2011 01:02 Edited at: 11th Jul 2011 01:02

What does the second number do?
First number -3 makes names goes descending.
Thanks
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 11th Jul 2011 02:22
IanM wrote what the second number does. It allows you to sort by as many fields as you want or by just one.

Quote: "
` Sort UDT array by...
` Field 3 (c) ascending
` Field 2 (b) descending
sort array MyArray(), 3, -2
"


If it's a positive number it's ascending (a-z and 0-9) and if it's a negative number it's descending (z-a and 9-0). The number itself is the field number so your code of "sort array MyArray(), -3, 2" will sort first by field 3 descending then sort by field 2 ascending.

toggy
21
Years of Service
User Offline
Joined: 6th Jun 2005
Location: Serbia
Posted: 11th Jul 2011 03:05
Quote: "
The number itself is the field number so your code of "sort array MyArray(), -3, 2" will sort first by field 3 descending then sort by field 2 ascending.
"

I'm trying to understand. Print command shows that there is sort in a field 3(string-names)from "Z to A" because -3. I see that.But, i see no sorting in field 2. Only way i got sorting by field 2 is when put number 2(positive or negative)in a form

Could you please show me example? Tnx
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 11th Jul 2011 04:21 Edited at: 11th Jul 2011 04:22
Think of multiple field sorting as "importance level". It'll sort by the first field you give it then sort by the next field and so on till it's done all the fields you want.

I changed your code to uses only strings for first, middle, and last name. It has two with the same last name and sorts by last and middle names... notice how they are before sorting and after.



toggy
21
Years of Service
User Offline
Joined: 6th Jun 2005
Location: Serbia
Posted: 11th Jul 2011 17:16
Yes, i got it now, thanks.
Just like phone book.First the sorting is done by last name, then by the name.Your example make it all clear,tnx.

Login to post a reply

Server time is: 2026-07-10 23:13:44
Your offset time is: 2026-07-10 23:13:44