SA-MP Forums Archive
[Include] Sort multi-dimensional arrays (enums supported) - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+---- Forum: Includes (https://sampforum.blast.hk/forumdisplay.php?fid=83)
+---- Thread: [Include] Sort multi-dimensional arrays (enums supported) (/showthread.php?tid=343172)

Pages: 1 2 3 4 5


Sort multi-dimensional arrays (enums supported) - Slice - 16.05.2012

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


Re: Sort multi-dimensional arrays (enums supported) - RyDeR` - 16.05.2012

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


Re: Sort multi-dimensional arrays (enums supported) - Slice - 16.05.2012

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.


Re: Sort multi-dimensional arrays (enums supported) - BlackBank - 16.05.2012

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


Re: Sort multi-dimensional arrays (enums supported) - richardcor91 - 16.05.2012

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?


Re: Sort multi-dimensional arrays (enums supported) - Slice - 16.05.2012

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.


Re: Sort multi-dimensional arrays (enums supported) - Niko_boy - 16.05.2012

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


Re: Sort multi-dimensional arrays (enums supported) - Slice - 16.05.2012

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!


Re: Sort multi-dimensional arrays (enums supported) - Niko_boy - 16.05.2012

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


Re: Sort multi-dimensional arrays (enums supported) - Wanted1900 - 16.05.2012

Great! Just what I was looking for!


Re: Sort multi-dimensional arrays (enums supported) - Jonny5 - 16.05.2012

Very nice slice!

great work as always.


AW: Sort multi-dimensional arrays (enums supported) - olaf137 - 16.05.2012

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


Re: Sort multi-dimensional arrays (enums supported) - Slice - 16.05.2012

@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);



Re: Sort multi-dimensional arrays (enums supported) - Niko_boy - 17.05.2012

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


Re: Sort multi-dimensional arrays (enums supported) - Slice - 17.05.2012

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.


Re: Sort multi-dimensional arrays (enums supported) - Ballu Miaa - 17.05.2012

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


Re: Sort multi-dimensional arrays (enums supported) - CyNiC - 17.05.2012

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


Re: Sort multi-dimensional arrays (enums supported) - Slice - 17.05.2012

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.


Re: Sort multi-dimensional arrays (enums supported) - TheArcher - 17.05.2012

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.


Re: Sort multi-dimensional arrays (enums supported) - Niko_boy - 17.05.2012

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