Raised This Month: $ Target: $400
 0% 

Sorting values


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
LambdaLambda
AlliedModders Donor
Join Date: Oct 2010
Location: London
Old 04-26-2014 , 22:57   Sorting values
Reply With Quote #1

Hey guys,

let's say I have a global array, each player has different value for it (forgot it's name [MAXPLAYERS+1]).

And what I would like to do is to sort these values from smaller to greater, and find the position of a player.

Thank you!

Last edited by LambdaLambda; 04-26-2014 at 23:35.
LambdaLambda is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 04-27-2014 , 00:45   Re: Sorting values
Reply With Quote #2

If you want to sort it, you're going to have to make a two-dimensional array and store the player userid (or client index) as one of the items in the second dimension... then use the SortCustom2D function to sort it by the correct dimension.

For example, this is how NativeVotes sorts its vote array. Note that both of the arguments passed to this function are references that have no value set and that it uses constants to define which item is which for the second index:
PHP Code:
Internal_GetResults(votes[][], &num_votes=0)
{
    if (!
Internal_IsVoteInProgress())
    {
        return 
0;
    }
    
    
// Since we can't have structs, we get "struct" with this instead
    
new num_items;
    
    
num_votes 0;
    
    for (new 
0g_Itemsi++)
    {
        new 
voteCount GetArrayCell(g_hVotesi);
        if (
voteCount 0)
        {
            
votes[num_items][VOTEINFO_ITEM_INDEX] = i;
            
votes[num_items][VOTEINFO_ITEM_VOTES] = voteCount;
            
num_votes += voteCount;
            
num_items++;
        }
    }
    
    
/* Sort the item list descending like we promised */
    
SortCustom2D(votesnum_itemsSortVoteItems);

    return 
num_items;
}

// Sort by votes descending
public SortVoteItems(a[], b[], const array[][], Handle:hndl)
{
    if (
b[VOTEINFO_ITEM_VOTES] == a[VOTEINFO_ITEM_VOTES])
    {
        return 
0;
    }
    else if (
b[VOTEINFO_ITEM_VOTES] > a[VOTEINFO_ITEM_VOTES])
    {
        return 
1;
    }
    else
    {
        return -
1;
    }

__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
LambdaLambda
AlliedModders Donor
Join Date: Oct 2010
Location: London
Old 04-28-2014 , 18:38   Re: Sorting values
Reply With Quote #3

Thanks for your replay!

I don't actually understand how this function work, will have to do more research on it, I think.

How is it sorting on example you've showed to me? If you could explain it to me better alittle, would be wonderful!
LambdaLambda is offline
bl4nk
SourceMod Developer
Join Date: Jul 2007
Old 04-28-2014 , 20:04   Re: Sorting values
Reply With Quote #4

Just look at the API page for SortCustom2D and the returns in the definition of SortFunc2D:
Code:
/**
 * Sort comparison function for 2D array elements (sub-arrays).
 * @note You may need to use explicit tags in order to use data properly.
 *
 * @param elem1            First array to compare.
 * @param elem2            Second array to compare.
 * @param array            Array that is being sorted (order is undefined).
 * @param hndl            Handle optionally passed in while sorting.
 * @return                -1 if first should go before second
 *                        0 if first is equal to second
 *                        1 if first should go after second
 */
funcenum SortFunc2D
{
    public(elem1[], elem2[], const array[][], Handle:hndl),
    public(String:elem1[], String:elem2[], const String:array[][], Handle:hndl),
};

Last edited by bl4nk; 04-28-2014 at 20:08. Reason: fix pasted text in code tags
bl4nk is offline
LambdaLambda
AlliedModders Donor
Join Date: Oct 2010
Location: London
Old 04-28-2014 , 21:09   Re: Sorting values
Reply With Quote #5

Okey. Let's say I'm okay with that. But how do I display the actual position?
LambdaLambda is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 04-30-2014 , 05:02   Re: Sorting values
Reply With Quote #6

Are you trying get "rank" position ?
example: If you store player points in global array using player index as array index,
after sorting that same array, you loose meaning of player index's.

You need create second array for that purpose.
Bacardi is offline
Bimbo1
Senior Member
Join Date: Jan 2010
Location: brazil
Old 04-30-2014 , 05:58   Re: Sorting values
Reply With Quote #7

Code:
SortPlayersPoints(const points[MAXPLAYERS+1], rank[MAXPLAYERS+1][2]){
//this is where you put your "forgot its name [MAXPLAYERS+1]". its output is the rank array, which should be input empty.
//rank array will be an array starting from 0 and [i][0] is a client index of the i position and [i][1] is that client's points.
//the rank array is sorted from smallest points to highest.

    new count = 0;
    for(new i = 1; i <= MaxClients; i++){

        if(IsClientConnected(i)){

            rank[count][0] = i;
            rank[count][1] = points[i];
            count++;

        }

    }
    SortIt(rank, count);

}

SortIt(rank[][], size, start = 0){
//you don't need to use this

    if(start == size){

        return;

    }
    new smallest = start;
    for(new i = start; i < size; i++){

        if(rank[i][1] < rank[smallest][1]){

            smallest = i;

        }

    }
    decl temp[2];
    temp[0] = rank[smallest][0];
    temp[1] = rank[smallest][1];
    rank[smallest][0] = rank[start][0];
    rank[smallest][1] = rank[start][1];
    rank[start][0] = temp[0];
    rank[start][1] = temp[1];
    SortIt(rank, size, start+1);

}

GetPlayerPosition(client, rank[MAXPLAYERS+1][2]){
// this will return the position a client is placed in the rank array, considering 0 place 1.

    for(new i = 1; i <= MaxClients; i++){

        if(rank[i][0] == client){

            return i+1; //array starts from 0, but position starts from 1

        }

    }
    return -1;

}
__________________
sie sind das essen und wir sind die jäger!
Bimbo1 is offline
LambdaLambda
AlliedModders Donor
Join Date: Oct 2010
Location: London
Old 04-30-2014 , 07:57   Re: Sorting values
Reply With Quote #8

Quote:
Originally Posted by Bacardi View Post
Are you trying get "rank" position ?
example: If you store player points in global array using player index as array index,
after sorting that same array, you loose meaning of player index's.

You need create second array for that purpose.
Oh, ok! Thank you, I think I will be good with that now.

@Bimbo1
thanks for that code, but I already made something simillar to that. I was curious how to use the function that Powerlord showed to me.

Thanks again, guys!
LambdaLambda is offline
zipcore
Veteran Member
Join Date: Mar 2010
Location: m_flZipcore
Old 05-16-2014 , 05:26   Re: Sorting values
Reply With Quote #9

Thanks . Thats usefull for a special teambalancer on my todo list
__________________
zipcore is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:07.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode