[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
#2

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

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

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

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]);
    }
}
Reply
#6

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

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

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

Is it better/faster than md-sort include?
Reply
#10

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

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


Forum Jump:


Users browsing this thread: 1 Guest(s)