SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Includes

Reply
 
Thread Tools Display Modes
Old 16/05/2012, 02:23 PM   #1
Slice
High-roller
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,835
Reputation: 1607
Default Sort multi-dimensional arrays (enums supported)

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 Code:
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 Code:
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:
Code:
[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

Last edited by Slice; 17/05/2012 at 01:41 PM.
Slice is offline   Reply With Quote
Old 16/05/2012, 02:48 PM   #2
RyDeR`
High-roller
 
RyDeR`'s Avatar
 
Join Date: Feb 2009
Location: Belgium
Posts: 2,929
Reputation: 696
Default Re: Sort multi-dimensional arrays (enums supported)

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

Sup?
RyDeR` is offline   Reply With Quote
Old 16/05/2012, 03:07 PM   #3
Slice
High-roller
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,835
Reputation: 1607
Default Re: Sort multi-dimensional arrays (enums supported)

Quote:
Originally Posted by RyDeR` View Post
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.
Slice is offline   Reply With Quote
Old 16/05/2012, 03:29 PM   #4
BlackBank
Gangsta
 
Join Date: Dec 2010
Location: The Netherlands
Posts: 524
Reputation: 437
Default Re: Sort multi-dimensional arrays (enums supported)

Nice done.
Now it's much easier to sort multi-array's, thanks.
BlackBank is offline   Reply With Quote
Old 16/05/2012, 03:32 PM   #5
richardcor91
Big Clucker
 
Join Date: Apr 2011
Location: Portugal
Posts: 80
Reputation: 30
Default Re: Sort multi-dimensional arrays (enums supported)

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?
richardcor91 is offline   Reply With Quote
Old 16/05/2012, 03:41 PM   #6
Slice
High-roller
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,835
Reputation: 1607
Default Re: Sort multi-dimensional arrays (enums supported)

Quote:
Originally Posted by richardcor91 View Post
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.
Slice is offline   Reply With Quote
Old 16/05/2012, 03:42 PM   #7
Niko_boy
High-roller
 
Niko_boy's Avatar
 
Join Date: Aug 2010
Location: Somewhere i belong
Posts: 1,338
Reputation: 138
Default Re: Sort multi-dimensional arrays (enums supported)

Loving Man <3
me was working on Sorting Stuff full day , and some what useful stuff you released thanks for this again Slice
__________________
nope[IMG]http://*******/1r0SOkH_[/IMG]
•••[CLOSED]LCS•Freeroam•DM•Stunts•••AutoArena [0.3z][No SkinShot][sixtytiger.com]Want a decent Attack Defend Gamemode?
N/A176.31.229.148:7830Get This! Attack-Defend(v2.3.1)
Niko_boy is offline   Reply With Quote
Old 16/05/2012, 03:43 PM   #8
Slice
High-roller
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,835
Reputation: 1607
Default Re: Sort multi-dimensional arrays (enums supported)

Quote:
Originally Posted by Niko_boy View Post
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!
Slice is offline   Reply With Quote
Old 16/05/2012, 03:48 PM   #9
Niko_boy
High-roller
 
Niko_boy's Avatar
 
Join Date: Aug 2010
Location: Somewhere i belong
Posts: 1,338
Reputation: 138
Default Re: Sort multi-dimensional arrays (enums supported)

aaaaaah thx for sharing =D and visualizing the idea
In a while,surely, i Actually gonna try it.
__________________
nope[IMG]http://*******/1r0SOkH_[/IMG]
•••[CLOSED]LCS•Freeroam•DM•Stunts•••AutoArena [0.3z][No SkinShot][sixtytiger.com]Want a decent Attack Defend Gamemode?
N/A176.31.229.148:7830Get This! Attack-Defend(v2.3.1)
Niko_boy is offline   Reply With Quote
Old 16/05/2012, 04:37 PM   #10
Wanted1900
Little Clucker
 
Join Date: Feb 2009
Posts: 43
Reputation: 0
Default Re: Sort multi-dimensional arrays (enums supported)

Great! Just what I was looking for!
Wanted1900 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
multi-dimensional arrays error? Admigo Scripting Help 3 17/11/2011 05:56 PM
multi-dimensional arrays must be fully initialized DVDK Help Archive 5 11/12/2010 04:40 PM
multi-dimensional arrays >? Kar Help Archive 3 02/08/2010 08:26 AM


All times are GMT. The time now is 10:43 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.