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

Damn that nice i will use it in future.
Reply
#22

that's realy nice keep up man
Reply
#23

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!
Reply
#24

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
Reply
#25

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.
Reply
#26

YES! It works

Thanks for the help and such a great script!
Reply
#27

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"
}
Reply
#28

Do you have admin_Level in any other enums?
Reply
#29

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
Reply
#30

Try "_:admin_Level".
Reply
#31

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
Reply
#32

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")
Reply
#33

How can i do the same with damages?:/

Return a list of player order by damages
Reply
#34

Nice include, keep up the good work
Reply
#35

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
Reply
#36

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

Thanks, keep it up!
Reply
#38

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?
Reply
#39

What other includes do you have?
Reply
#40

#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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)