[Include] inline-sort.inc - The solution to all your sorting needs
#1

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
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 2 Guest(s)