[Include] Sort multi-dimensional arrays (enums supported)
#1

Hey,

This is a small include that adds a function for sorting multi-dimensional arrays easily! The function works as great for huge arrays just as it does for small arrays. The reason for this is it never actually touches the contents of the array - only the array's internal header.

You can also sort by strings and specify the sort order.

Credits to RyDeR` for his quickSort function.

Example 1:
pawn Код:
enum e_TEST_ARRAY {
          IntValue,
    Float:FloatValue,
          StringValue[48]
};

new
    g_TestArray[][e_TEST_ARRAY] = {
        // data...
    }
;

main() {
    SortDeepArray(g_TestArray, FloatValue);
   
    // g_TestArray is now sorted by g_TestArray[][FloatValue]
}
Example 2:
pawn Код:
main() {
    new array[][2] = {
        {512, 111},
        {-42, 222},
        {932, 333},
        {  9, 444},
        {-90, 555}
    };
   
    SortDeepArray(array, 0);
   
    printf("Sorted \"array\":");
   
    for (new i = 0; i < sizeof(array); i++) {
        printf("    array[%d] = {%d, %d}", i, array[i][0], array[i][1]);
    }
}
Output:
Код:
[16:25:05] Sorted "array":
[16:25:05]     array[0] = {-90, 555}
[16:25:05]     array[1] = {-42, 222}
[16:25:05]     array[2] = {9, 444}
[16:25:05]     array[3] = {512, 111}
[16:25:05]     array[4] = {932, 333}
Live demo: http://slice-vps.nl/ppg/#gist=1128054394f01cbf5ac3 (updated!)
Download: https://raw.github.com/oscar-broman/...er/md-sort.inc
Reply
#2

Nice work! Just one question though, why did you stop using the Hungarian notation? Not that it really matters, just curious.
Reply
#3

Quote:
Originally Posted by RyDeR`
Посмотреть сообщение
Nice work! Just one question though, why did you stop using the Hungarian notation? Not that it really matters, just curious.
I feel it's just superfluous, and it slowed me down. I can much quicker skim through my own code as well. Using tags can get you far enough, imo.
Reply
#4

Nice done.
Now it's much easier to sort multi-array's, thanks.
Reply
#5

So basically you're sorting the data by changing the order of the pointers, that's why you don't need to "move" data, or no?
Reply
#6

Quote:
Originally Posted by richardcor91
Посмотреть сообщение
So basically you're sorting the data by changing the order of the pointers, that's why you don't need to "move" data, or no?
Correct. If you have a look at the code, you'll see there's a function called ExchangeArraySlots, which is where the magic happens. It changes the pointer at array[slot1] to array[slot2] and vice versa.
Reply
#7

Loving Man <3
me was working on Sorting Stuff full day , and some what useful stuff you released thanks for this again Slice
Reply
#8

Quote:
Originally Posted by Niko_boy
Посмотреть сообщение
Loving Man <3
me was working on Sorting Stuff full day , and some what useful stuff you released thanks for this again Slice
I actually got the idea from your post in useful functions!
Reply
#9

aaaaaah thx for sharing =D and visualizing the idea
In a while,surely, i Actually gonna try it.
Reply
#10

Great! Just what I was looking for!
Reply
#11

Very nice slice!

great work as always.
Reply
#12

Is there a way to sort an array without changing the order of the array which stores the data.
And to save the sorted IDs of the data-array in another array.

Example:
Код:
new dataarray[5][2] = {   // Stores the data
        {512, 111},  // 0
        {-42, 222},  // 1
        {932, 333},  // 2
        {  9, 444},  // 3
        {-90, 555}   // 4
    };
new sortarray[5];  // Will store the order of the sorted data
SortArray(dataarray, 0, sortarray);
sortarray stores now the order of the sorted dataarray.
The value is the index of the item in the dataarray.

IndexValue
04
11
23
30
42
With this feature you could sort an array like Score[MAX_PLAYERS] without changing the order of the playerids.

I hope you understand what i mean
Reply
#13

@olaf137: Unfortunately that's not possible with this include. I might add this in the future.

Update!

Sort order can now be specified.
Код:
SortDeepArray(my_array, index, .order = SORT_ASC); // default
SortDeepArray(my_array, index, .order = SORT_DESC);
Strings can now be sorted! And I have to say I'm surprised how fast it is.
Код:
SortDeepArray(my_array, string:index);
SortDeepArray(my_array, string:index, .ignorecase = false); // default
SortDeepArray(my_array, string:index, .order = SORT_DESC, .ignorecase = true);
Reply
#14

owaa an update good
Код:
 ...\pawno\include\md-sort.inc(26) : error 020: invalid symbol name ""
what about this error "__"
Reply
#15

Seems like there's an include removing the string tag, include md-sort before any other stuff. I'll look into best ways to deal with this.
Reply
#16

Oww okay. Nice work Slice , Thanks to Niko for his idea. xD Rep+6 Both.
Reply
#17

Excellent, I was a long time wanting something clean like this.
Reply
#18

Quote:
Originally Posted by ******
Посмотреть сообщение
Yeah, YSI does that, sorry... I'm not actually sure if it needs removing, but it is used in there to do some clever macros without giving tag mismatch warnings (see the pre-processor post on them). I tend to use macros to detect the tag before it is removed and then generate different code as a result, stripping the "string:" tag from the declaration at the end so that normal strings can be passed.
Ahh, that's what I did, too. I just forgot to remove the tag comparisons. Ive updated the include now.
Reply
#19

Whaaa you saved my life, i really like this dude i was waiting for a script like this for a while. Also i'm waiting for olaf137 said and if is that possibile.
Reply
#20

okay so that's fixed now Slice ?
i'll try it again.
Reply


Forum Jump:


Users browsing this thread: 19 Guest(s)