SA-MP Forums Archive
[Include] inline-sort.inc - The solution to all your sorting needs - 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] inline-sort.inc - The solution to all your sorting needs (/showthread.php?tid=602584)



inline-sort.inc - The solution to all your sorting needs - Slice - 09.03.2016

Hey guys, it's been a while.

This include allows you to sort a plain array using your own, custom comparison. The comparison happens in the same place you run the sort!

I have previously done a similar thing in md-sort with the "comparators", but those sorts require external function calls and thus the code for the sorting is in 2 places.

The include has special helpers to make it easy to sort players. Here's an example:
pawn Код:
new g_Money[MAX_PLAYERS];

public OnSomething()
{
    // Sort all players based on the value in g_Money
    new PlayerArray<top_money>;

    sortPlayersInline top_money => (R = l > r) {
        R = g_Money[l] > g_Money[r];
    }

    forPlayerArray (top_money => playerid) {
        printf("%d has $%d", playerid, g_Money[playerid]);
    }
}
Another example, not using helper macros/functions:
pawn Код:
public OnSomething()
{
    new players_sorted[MAX_PLAYERS] = {0, 1, ...};

    sortInline players_sorted => (R = left > right) {
        new lc = IsPlayerConnected(left);
        new rc = IsPlayerConnected(right);
   
        // Is one of the players disconnected? Put the disconnected player below the connected.
        if (lc != rc) {
            R = lc > rc;
        } else {
            R = GetPlayerScore(left) > GetPlayerScore(right);
        }
    }

    for (new i = 0; i < sizeof(players_sorted); i++) {
        new playerid = players_sorted[i];
        if (!IsPlayerConnected(playerid)) break;

        printf("score of %d = %d", playerid, GetPlayerScore(playerid));
    }
}
This include only sorts one-dimensional arrays; instead of sorting a multi-dimensional array, you can simply sort an array of indexes.

Tell me what you wish to sort and I will show you how!

GitHub: https://github.com/oscar-broman/samp-inline-sort


Re: inline-sort.inc - The solution to all your sorting needs - Crystallize - 09.03.2016

Nice include Slice , shouldn't this be on "Include" section instead of Filterscript :3?


Re: inline-sort.inc - The solution to all your sorting needs - Slice - 09.03.2016

Quote:
Originally Posted by Wizzard2H
Посмотреть сообщение
Nice include Slice , shouldn't this be on "Include" section instead of Filterscript :3?
Right, yeah, I was in the wrong section.

dugi, I know you are reading this. Move it please!


Re: inline-sort.inc - The solution to all your sorting needs - Pottus - 09.03.2016

Out of all these releases you fuxed up and couldn't sort out where to put the sorting include


Re: inline-sort.inc - The solution to all your sorting needs - Slice - 09.03.2016

I've updated the include with player helpers now. It's now very easy (and more efficient) to sort players!

Here's an example how to sort players based on the value in g_Money:
pawn Код:
new g_Money[MAX_PLAYERS];

public OnSomething()
{
    // Sort all players based on the value in g_Money
    new PlayerArray<top_money>;

    sortPlayersInline top_money => (R = l > r) {
        R = g_Money[l] > g_Money[r];
    }

    forPlayerArray (top_money => playerid) {
        printf("%d has $%d", playerid, g_Money[playerid]);
    }
}



Re: inline-sort.inc - The solution to all your sorting needs - kristo - 10.03.2016

Does this include confuse the compiler as well? I remember that it thought the md-sort include had a recursive function in it.


Re: inline-sort.inc - The solution to all your sorting needs - Slice - 10.03.2016

The inline sorting is not recursive. It uses a bottom-up version of merge sort. The function called MergeSort is recursive, but it's not used by default.


Re: inline-sort.inc - The solution to all your sorting needs - Fairuz - 21.08.2016

PHP код:
sort-inline.inc(123) : warning 201redefinition of constant/macro (symbol "PlayerArray%0<%1>"
includes that I use,

PHP код:
#include <a_mysql>
#include <timestamptodate>
#include <sort-inline>
#include <YSI\y_timers>
#include <YSI\y_hooks>
#include <YSI\y_iterate>
#include <YSI\y_commands>
#include <YSI\y_flooding>
#include <YSI\y_groups>
#include <YSI\y_areas>
#include <YSI\y_vehicledata>
#include <streamer>
#include <irc>
#include <antiadvertising> 
Most likely that It's because YSI,right?How do I fix this problem?

EDIT:Renaming PlayerArray to PlayerArrayEx,it worked.but tell me it's a good idea or not.


Re: inline-sort.inc - The solution to all your sorting needs - ball - 21.08.2016

Is it better/faster than md-sort include?


Re: inline-sort.inc - The solution to all your sorting needs - Slice - 21.08.2016

@Bradley7: Yeah that's a good idea. YSI has another thing called PlayerArray.

@ball: It's more convenient, but the speed is similar.