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


Re: Sort multi-dimensional arrays (enums supported) - AMEENAMEEN - 04.06.2012

Damn that nice i will use it in future.


Re: Sort multi-dimensional arrays (enums supported) - S_ILYa™ - 04.06.2012

that's realy nice keep up man


Re: Sort multi-dimensional arrays (enums supported) - AndreT - 10.07.2012

I realized just now that I could save a lot of my own time (and a lot of the server processing time as well) by using this.

Thank you, quite the lifesaver!


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

Awesome update!

You can now create custom sort functions that can access, for example, multiple fields of an enumerated array and decide what goes where.

If you read the comments here, you'll see what I mean:
https://gist.github.com/1677d89435a976d80c8e


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

With the latest version, you can sort into other arrays (only for the comparator sorts atm, but that's just the opposite of a limitation).

First you'll need a comparator function:
pawn Код:
Comparator:CompareDistanceToFinish(left[E_RACE_DATA], right[E_RACE_DATA]) {
    // Returning negative means "left goes above"
    // Positive means "left goes below"
    // floatcmp returns -1 if right > left, 0 if equal, and 1 if left > right
    return floatcmp(left[DistanceToFinish], right[DistanceToFinish]);
}
Then you'll need a temporary array that you sort into:
pawn Код:
new sorted_racers[MAX_PLAYERS];

SortArrayUsingComparator(gPlayerRaceData, CompareDistanceToFinish) => sorted_racers;

for (new i = 0; i < sizeof(sorted_racers); i++) {
    new playerid = sorted_racers[i];
   
    if (!IsPlayerConnected(playerid))
        continue;
   
    // do stuff with "playerid"
}
There is, however, one little complication. Currently, md-sort has no idea what players are. It will blindly sort the whole array, regardless whether the player is connected.
So you won't be able to simply do sorted_racers[2] to get the 3rd top (as old values from disconnected players could be there). One workaround for this is when players disconnect, set the distance to like 9999.


Re: Sort multi-dimensional arrays (enums supported) - [HLF]Southclaw - 23.07.2012

YES! It works

Thanks for the help and such a great script!


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

Glad you got it working.

I just released a new version that deals with player array sorting much more efficiently (DL at first post).

Basically, change your sorting code to this:
pawn Код:
new sorted_racers[MAX_PLAYERS];

SortArrayUsingComparator(gPlayerRaceData, CompareDistanceToFinish, SORT_IS_PLAYERS) => sorted_racers;

for (new i = 0; i < sizeof(sorted_racers); i++) {
    new playerid = sorted_racers[i];
   
    // All valid player IDs are at the start of the array, so the first invalid one means the rest will be, too.
    if (playerid == INVALID_PLAYER_ID)
        break;
   
    // do stuff with "playerid"
}



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

Do you have admin_Level in any other enums?


Re: Sort multi-dimensional arrays (enums supported) - [HLF]Southclaw - 14.09.2012

I'm getting an irritating error on compile!

warning 229: index tag mismatch (symbol "gAdminData")

I didn't get this on the race distance sorting, this is for an /admins dialog that shows admins in order of level.

Declaration:

pawn Code:
enum E_ADMIN_DATA
{
    admin_Name[MAX_PLAYER_NAME],
    admin_Level
}
new
    gAdminData[MAX_ADMIN][E_ADMIN_DATA];
The error line:

pawn Code:
SortDeepArray(gAdminData, admin_Level, .order = SORT_DESC);
Am I doing something wrong? I guess this error is nothing to be worried about really, but it's still annoying me! I prefer to have a clean compile console


Re: Sort multi-dimensional arrays (enums supported) - Y_Less - 14.09.2012

Try "_:admin_Level".


Re: Sort multi-dimensional arrays (enums supported) - [HLF]Southclaw - 14.09.2012

I tried that already, oddly it gives two more errors, I looked in to the include source and saw that it's also a few macros too.
Code:
warning 229: index tag mismatch (symbol "gAdminData")
warning 213: tag mismatch
warning 213: tag mismatch



Re: Sort multi-dimensional arrays (enums supported) - [HLF]Southclaw - 14.09.2012

No, I just searched all my includes (well, my entire SA:MP folder) with notepad++ search in files and it didn't crop up elsewhere, it's only defined in the gamemode, used there and in one include.


Edit: Found out the reason, it seemed to be case sensitive:

pawn Code:
#include <a_samp>
#include <md-sort>

enum some_enum
{
    string[32],
    integer
}

new gAdminData[1][some_enum];


main()
{
    SortDeepArray(gAdminData, integer, .order = SORT_DESC);
}
That code works fine, but try this code:
pawn Code:
#include <a_samp>
#include <md-sort>

enum SOME_ENUM
{
    string[32],
    integer
}

new gAdminData[1][SOME_ENUM];


main()
{
    SortDeepArray(gAdminData, integer, .order = SORT_DESC);
}
Not sure why that would happen but hey, it fixed my problem (renamed to "e_admin_data")


Re: Sort multi-dimensional arrays (enums supported) - ReVo_ - 06.10.2012

How can i do the same with damages?:/

Return a list of player order by damages


AW: Sort multi-dimensional arrays (enums supported) - Littl3j0hNy - 12.11.2012

Nice include, keep up the good work


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

Quote:
Originally Posted by [HLF]Southclaw
Посмотреть сообщение
I am wondering about this feature, it would be nice if it was a speed improvement (to looping or assigning a new variable if you don't want to actually edit the order of the original data)
http://forum.sa-mp.com/showthread.ph...32#post2007632


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

Yes - use the custom comparator (example in this topic).


Re: Sort multi-dimensional arrays (enums supported) - Christ - 25.01.2013

Thanks, keep it up!


Re: Sort multi-dimensional arrays (enums supported) - habl - 29.01.2013

Hi,

I'm trying to use this include, but getting a bunch of errors when compiling:

Код:
D:\samp\server\pawno\include\md-sort.inc(0) : error 075: input line too long (after substitutions)
D:\samp\server\pawno\include\md-sort.inc(1) : error 075: input line too long (after substitutions)
D:\samp\server\pawno\include\md-sort.inc(7) : error 010: invalid function or declaration
D:\samp\server\pawno\include\md-sort.inc(12) : error 001: expected token: "-identifier-", but found "#emit"
D:\samp\server\pawno\include\md-sort.inc(12) : error 075: input line too long (after substitutions)
D:\samp\server\pawno\include\md-sort.inc(13) : error 010: invalid function or declaration
D:\samp\server\pawno\include\md-sort.inc(15) : error 075: input line too long (after substitutions)
D:\samp\server\pawno\include\md-sort.inc(17) : error 010: invalid function or declaration
D:\samp\server\pawno\include\md-sort.inc(17) : error 010: invalid function or declaration
D:\samp\server\pawno\include\md-sort.inc(22) : error 038: extra characters on line
D:\samp\server\pawno\include\md-sort.inc(22) : error 075: input line too long (after substitutions)
D:\samp\server\pawno\include\md-sort.inc(24) : warning 203: symbol is never used: "or"
D:\samp\server\pawno\include\md-sort.inc(24) : error 013: no entry point (no public functions)
I can't really figure out what i'm doing wrong, only thing I did was downloading the include and actually including it to my gamemode.

Any clue?


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

What other includes do you have?


Re: Sort multi-dimensional arrays (enums supported) - habl - 30.01.2013

#include <Junkbuster>
#include <Dini>
#include <fuckCleo>

Tried to disable them all, didn't made a difference.

Even more strange is, I also tried it on a fresh sa-mp server install on the standard gamemode lvdm and got the same errors. I really don't understand why this is happening.