Raised This Month: $ Target: $400
 0% 

[Solved] Sorting Arrays


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
BhK
Junior Member
Join Date: Dec 2014
Location: India
Old 01-09-2016 , 05:15   [Solved] Sorting Arrays
Reply With Quote #1

I know sorting an array is common and must be known for a beginner, But i am a little confuse here i tried some methods which came in my mind but failed !
Now,
g_Array[10] = {0,5,6,1,9,2,4,6,7,2}
if we sort it in descending order we get
9
7
6
5
...
0
but i want to store their place holders too
g_Holders[10]
4 ( cause it contains 9)
8 ( cause it contains 7)
and so on ...

the method i used to sort array (which i m currently learning in my school),
you can tell me another method too, if its working i would have no problem in using it !

PHP Code:
new g_Array[10] = {0,5,6,1,9,2,4,6,7,2}
new 
g_Temp
server_print
("Array Test")
for(new 
k=1;k<10;k++)
{
    for(new 
j=0;j<(10-k);j++)
    {
        if(
g_Array[j]<g_Array[j+1])
        {
            
g_Temp g_Array[j];
            
g_Array[j] = g_Array[j+1];
            
g_Array[j+1] = g_Temp
        
}
    }
}
for(new 
j=0;j<10;j++)
{
    
server_print("%i",g_Array[j]);

how can i get their place holders ?

Last edited by BhK; 01-18-2016 at 02:02. Reason: Solved
BhK is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 01-09-2016 , 08:06   Re: [Help] Sorting Arrays
Reply With Quote #2

You want to get g_array[0]=0 , g_array[1]=1 and so next?
Place number = value number ,yes?

Last edited by siriusmd99; 01-09-2016 at 08:06.
siriusmd99 is offline
BhK
Junior Member
Join Date: Dec 2014
Location: India
Old 01-09-2016 , 08:15   Re: [Help] Sorting Arrays
Reply With Quote #3

g_Array[10] = {0,5,6,1,9,2,4,6,7,2}
//index 0 1 2 3 4 5 6 7 8 9
After sorting
g_Array[10] = {9,7,6,6,5,4,2,2,1,0}
And i want their index stored in holder like
g_Holder[10] = {4,8,2,7,1,6,5,9,3,0}

sorry i am not getting a proper word for index!
__________________
Preparing to release my plugins..
BhK is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 01-09-2016 , 08:25   Re: [Help] Sorting Arrays
Reply With Quote #4

Man, explain what are you trying to do because your methods are uneficient.

Describe what your plugin does.
siriusmd99 is offline
BhK
Junior Member
Join Date: Dec 2014
Location: India
Old 01-09-2016 , 08:39   Re: [Help] Sorting Arrays
Reply With Quote #5

Lets assume
g_Kills[33]
when player kills someone it will increment i.e g_Kills[id]++ (id = index)
now lets assume that at the end of the round i want to show the players names with maximum kills at the top and minimum kills at the bottom!
for doing this it is obvious that we will sort g_Kills[33] !
After sorting it how to get the player index ?
if u didnt understand this just show me a way to display players list with maximum kills at the top and minimum kills at the bottom !
__________________
Preparing to release my plugins..

Last edited by BhK; 01-09-2016 at 08:41.
BhK is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 01-09-2016 , 10:31   Re: [Help] Sorting Arrays
Reply With Quote #6

Try :
PHP Code:
#include <amxmodx>

new g_score[33]

public 
plugin_init()
{
    
register_plugin("RoundEnd Score" "1.0" "siriusmd99" )
    
register_logevent("Round_End_Score"2"1=Round_End")
    
register_event("DeathMsg""sDeathMsg""a")
}

public 
client_connect(id) {g_score[id] = 0;}
public 
client_disconnect(id) {g_score[id] = 0;}

public 
sDeathMsg()
{
    new 
Killer read_data(1);
    new 
Victim read_data(2);
    if(
Killer != Victim)
        
g_score[Killer]++
}    

public 
Round_End_Score(){
    
    new 
sName[32], sBuffer[1024]
    new 
iLen
    
new scoretemp
    
new bool:ismax
    
    iLen 
+= formatex(sBuffer[iLen], charsmax(sBuffer) - iLen"Top Players Score:^n")
    
    while(!
is_empty())
    {
        for(new 
1sizeof(g_score); i++)
        {   
            
score g_score[i]
            
ismax true
            
for(new 1sizeof(g_score); j++)
            {    
                
temp g_score[j]
                if(
score<temp)
                {
                    
ismax false
                    
break
                }  
            }
            if(
ismax)
            {
                if(
is_user_connected(i) && score 0)
                {
                    
get_user_name(isName31)
                    
iLen += formatex(sBuffer[iLen], charsmax(sBuffer) - iLen"%s - %d^n"sNamescore)
                }
                
g_score[i]=0
            
}
        }            
    }
    
set_hudmessage(2552552550.020.2406.010.0__, -1)
    
show_hudmessage(0sBuffer)    
}


bool:is_empty()
{
for(new 
1sizeof(g_score); n++)
    if(
g_score[n])
        return 
false;    
    
return 
true;

I didn't sort array , just displayed live the score of players.

Last edited by siriusmd99; 01-09-2016 at 10:45.
siriusmd99 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-09-2016 , 10:37   Re: [Help] Sorting Arrays
Reply With Quote #7

This is done using a 2-dimension array and SortCustom2D() function. In one dimension you store the player ID and in the other the kills value.

Result:
Code:
1. id=1 kills=199
2. id=16 kills=102
3. id=5 kills=55
4. id=12 kills=53
5. id=2 kills=52
6. id=13 kills=50
7. id=21 kills=23
PHP Code:
#define MAX_PLAYERS 32

enum _:KillData
{
    
PlayerID,
    
PlayerKills
}
new 
g_KillDataMAX_PLAYERS ][ KillData ];

public 
SortDataExample()
{
    
//Load random player ID's and kill data into array
    
g_KillData21 ][ PlayerID ] = 21;
    
g_KillData21 ][ PlayerKills ] = 23;
    
    
g_KillData][ PlayerID ] = 5;
    
g_KillData][ PlayerKills ] = 55;
    
    
g_KillData16 ][ PlayerID ] = 16;
    
g_KillData16 ][ PlayerKills ] = 102;
    
    
g_KillData][ PlayerID ] = 1;
    
g_KillData][ PlayerKills ] = 199;
    
    
g_KillData][ PlayerID ] = 2;
    
g_KillData][ PlayerKills ] = 52;
    
    
g_KillData12 ][ PlayerID ] = 12;
    
g_KillData12 ][ PlayerKills ] = 53;
    
    
g_KillData13 ][ PlayerID ] = 13;
    
g_KillData13 ][ PlayerKills ] = 50;
    
    
SortCustom2Dg_KillData sizeofg_KillData ) , "stats_custom_compare" );
    
    for ( new 
sizeofg_KillData ) ; i++ )
    {
        if ( 
g_KillData][ PlayerID ] )
        {
            
server_print"%d. id=%d kills=%d" i+g_KillData][ PlayerID ] , g_KillData][ PlayerKills ] );
        }
    }
}

public 
stats_custom_compare(elem1[],elem2[])
{
    if(
elem1[1] > elem2[1]) return -1;
    else if(
elem1[1] < elem2[1]) return 1;
    
    return 
0;

__________________

Last edited by Bugsy; 01-09-2016 at 10:39.
Bugsy is offline
BhK
Junior Member
Join Date: Dec 2014
Location: India
Old 01-09-2016 , 13:12   Re: [Help] Sorting Arrays
Reply With Quote #8

Quote:
Originally Posted by Bugsy View Post
This is done using a 2-dimension array and SortCustom2D() function. In one dimension you store the player ID and in the other the kills value.

Result:
Code:
1. id=1 kills=199
2. id=16 kills=102
3. id=5 kills=55
4. id=12 kills=53
5. id=2 kills=52
6. id=13 kills=50
7. id=21 kills=23
PHP Code:
#define MAX_PLAYERS 32

enum _:KillData
{
    
PlayerID,
    
PlayerKills
}
new 
g_KillDataMAX_PLAYERS ][ KillData ];

public 
SortDataExample()
{
    
//Load random player ID's and kill data into array
    
g_KillData21 ][ PlayerID ] = 21;
    
g_KillData21 ][ PlayerKills ] = 23;
    
    
g_KillData][ PlayerID ] = 5;
    
g_KillData][ PlayerKills ] = 55;
    
    
g_KillData16 ][ PlayerID ] = 16;
    
g_KillData16 ][ PlayerKills ] = 102;
    
    
g_KillData][ PlayerID ] = 1;
    
g_KillData][ PlayerKills ] = 199;
    
    
g_KillData][ PlayerID ] = 2;
    
g_KillData][ PlayerKills ] = 52;
    
    
g_KillData12 ][ PlayerID ] = 12;
    
g_KillData12 ][ PlayerKills ] = 53;
    
    
g_KillData13 ][ PlayerID ] = 13;
    
g_KillData13 ][ PlayerKills ] = 50;
    
    
SortCustom2Dg_KillData sizeofg_KillData ) , "stats_custom_compare" );
    
    for ( new 
sizeofg_KillData ) ; i++ )
    {
        if ( 
g_KillData][ PlayerID ] )
        {
            
server_print"%d. id=%d kills=%d" i+g_KillData][ PlayerID ] , g_KillData][ PlayerKills ] );
        }
    }
}

public 
stats_custom_compare(elem1[],elem2[])
{
    if(
elem1[1] > elem2[1]) return -1;
    else if(
elem1[1] < elem2[1]) return 1;
    
    return 
0;

Thanks this is what i was looking for
Just 1 question why used %d while array is integer.
__________________
Preparing to release my plugins..
BhK is offline
wickedd
Veteran Member
Join Date: Nov 2009
Old 01-09-2016 , 13:26   Re: [Help] Sorting Arrays
Reply With Quote #9

Quote:
Originally Posted by BhK View Post
Thanks this is what i was looking for
Just 1 question why used %d while array is integer.
Because %i and %d are used for integers.
__________________
Just buy the fucking game!!!!
I hate No-Steamers and lazy ass people.
wickedd is offline
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 01-09-2016 , 16:33   Re: [Help] Sorting Arrays
Reply With Quote #10

%s means string, %d means integer.
Look to ASCII table , find it on google images.

For example u have an array g_numbers with
g_numbers[0]= 65

Then %d will print 65 but %s will print char value, in ASCII ,65 is 'A'.

So %d - 65
%s - A
siriusmd99 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 09:25.


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