05.10.2014, 17:04
Quote:
Hi guys!
Can someone show me a short example how i can sort players in a list by checking a variable? Like, if the player has a score like 20 and the other one has score 10 - How can i use a cmd to sort them Descending after who the leader is? Leader: 20 Second place: 10 Third place .... And so on. An Example could be a list sorting the richest players in a list, after who the leader is: Richest: 999999999 second slot: 9999999 And so on.. - What is required? Is this possible, and how can i do it? Thanks! |
pawn Code:
//somewhere else: this is basically to put the list of current players on an array (it's unorganized by thevariable you want, but it's organized by player ids...
new array[MAX_PLAYERS], num=0;
for(new i=0; i<MAX_PLAYERS; i++)
{
if(!IsPlayerConnected(i))
continue;
array[num] = i;
num++;
}
array=bubblesort(aray, num);
stock bubblesort(a[], num)
{
new aux; //we'll need this auxiliar variable
for(new i=0; i<num; i++) //basically we'll need to search the array this ammount of times
{
for(new j=0; j<num-i; j++) //we'll put the lowest number in the end of the array, so we'll need to check which one it is and push it back, this loop will do the trick
{
if(GetPlayerScore(a[j])<GetPlayerScore(a[j+1])) //if the variable of j is lower than the adjacent position, then we should swap them, so the higher comes first.
{
aux=a[j+1];//save the adjacent one in the auxiliar variabe
a[j+1]=a[j];//reset the adjacent cell
a[j]=aux;//reset the current cell
}
}
}
}
/* An example of what might happen in the bubblesort stock function
ID SCORE
1 12
5 25
6 0
12 200
13 10
15 300
i j j(score) j+1 j+1(score) a a(scores)
- - - 1 5 6 12 13 15 12 25 0 200 10 300
0 0 12 - " "
0 0 12 1 25 5 1 6 12 13 15 25 12 0 200 10 300
0 1 12 2 0 5 1 6 12 13 15 25 12 0 200 10 300
0 2 0 3 200 5 1 12 6 13 15 25 12 200 0 10 300
0 3 0 4 10 5 1 12 13 6 15 25 12 200 10 0 300
0 4 0 5 300 5 1 12 13 15 6 25 12 200 10 300 0
1 0 25 1 12 5 1 12 13 15 6 25 12 200 10 300 0
1 1 12 2 200 5 12 1 13 15 6 25 200 12 10 300 0
1 2 12 3 10 5 12 1 13 15 6 25 200 12 10 300 0
1 3 10 4 300 5 12 1 15 13 6 25 200 12 300 10 0
2 0 25 1 200 12 5 1 15 13 6 200 25 12 300 10 0
2 1 25 2 12 12 5 1 15 13 6 200 25 12 300 10 0
2 2 12 3 300 12 5 15 1 13 6 200 25 300 12 10 0
3 0 200 1 25 12 5 15 1 13 6 200 25 300 12 10 0
3 1 25 2 300 12 15 5 1 13 6 200 300 25 12 10 0
4 0 200 1 300 15 12 5 1 13 6 300 200 25 12 10 0
*/