Raised This Month: $51 Target: $400
 12% 

Solved Sort Array but preserve index


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
_GamerX
AlliedModders Donor
Join Date: Jun 2011
Location: Fun Server
Old 09-30-2018 , 20:42   Sort Array but preserve index
Reply With Quote #1

I use this my code for sort best players on server but how to preserve default indexes for allotment SteamID player from second array?

I now use Array.Copy for store default indexes and Array.FindValue but if two and more indexes have same value so it's inaccurate. Please help, Thanks you.

PHP Code:
public void LoadTopPlayer()
{
    
int count;
    
char filename[32], buffer[32];
    
ArrayList TopPlayers[2];
    
ArrayList DefTopPlayers;
    
TopPlayers[0] = new ArrayList();
    
TopPlayers[1] = new ArrayList(64);
    
BuildPath(Path_SMg_sFilePathsizeof(g_sFilePath), "data/players/");
    
DirectoryListing dir OpenDirectory(g_sFilePath);
    while(
dir.GetNext(filenamesizeof(filename)))
    {
        if(
StrContains(filename"765611") != -1)
        {
            
KeyValues kv = new KeyValues("PlayerData");
            
BuildPath(Path_SMg_sFilePathsizeof(g_sFilePath), "data/players/%s"filename);
            
kv.ImportFromFile(g_sFilePath);
            if(
kv.JumpToKey("PlayerInfo"true))
            {
                
TopPlayers[0].Push(kv.GetNum("xp"0));
                
TopPlayers[1].PushString(filename);
            }
            
kv.Rewind();
            
delete kv;
            
count++;
        }
    }
    
DefTopPlayers TopPlayers[0].Clone();
    
SortADTArray(TopPlayers[0], Sort_DescendingSort_Integer);
    
TopPlayers[1].GetString(DefTopPlayers.FindValue(TopPlayers[0].Get(0)), buffersizeof(buffer));
    
CPrintToChatAll("%i - %s"TopPlayers[0].Get(0), buffer);
    
TopPlayers[1].GetString(DefTopPlayers.FindValue(TopPlayers[0].Get(1)), buffersizeof(buffer));
    
CPrintToChatAll("%i - %s"TopPlayers[0].Get(1), buffer);
    
TopPlayers[1].GetString(DefTopPlayers.FindValue(TopPlayers[0].Get(2)), buffersizeof(buffer));
    
CPrintToChatAll("%i - %s"TopPlayers[0].Get(2), buffer);
    
TopPlayers[1].GetString(DefTopPlayers.FindValue(TopPlayers[0].Get(3)), buffersizeof(buffer));
    
CPrintToChatAll("%i - %s"TopPlayers[0].Get(3), buffer);
    
TopPlayers[1].GetString(DefTopPlayers.FindValue(TopPlayers[0].Get(4)), buffersizeof(buffer));
    
CPrintToChatAll("%i - %s"TopPlayers[0].Get(4), buffer);

__________________

Last edited by _GamerX; 10-06-2018 at 05:52.
_GamerX is offline
Send a message via ICQ to _GamerX Send a message via Skype™ to _GamerX
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 09-30-2018 , 22:28   Re: Sort Array but preserve index
Reply With Quote #2

You can combine all the data needed into 1 ArrayList by using an enum struct, then use SortADTArrayCustom to sort by whatever value you want.

Last edited by hmmmmm; 09-30-2018 at 22:28.
hmmmmm is offline
_GamerX
AlliedModders Donor
Join Date: Jun 2011
Location: Fun Server
Old 10-01-2018 , 02:30   Re: Sort Array but preserve index
Reply With Quote #3

a pity that can not be two slots for value in one index in ArrayList...
__________________
_GamerX is offline
Send a message via ICQ to _GamerX Send a message via Skype™ to _GamerX
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 10-01-2018 , 03:44   Re: Sort Array but preserve index
Reply With Quote #4

Quote:
Originally Posted by _GamerX View Post
a pity that can not be two slots for value in one index in ArrayList...
Huh?

Blocksize does exactly this.
__________________
Neuro Toxin is offline
_GamerX
AlliedModders Donor
Join Date: Jun 2011
Location: Fun Server
Old 10-01-2018 , 03:49   Re: Sort Array but preserve index
Reply With Quote #5

Quote:
Originally Posted by Neuro Toxin View Post
Huh?

Blocksize does exactly this.
How any example please?
__________________
_GamerX is offline
Send a message via ICQ to _GamerX Send a message via Skype™ to _GamerX
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 10-01-2018 , 03:56   Re: Sort Array but preserve index
Reply With Quote #6

When you inititalise the ArrayList set the blocksize.

When you call GetSet parse the block you want to access.
__________________
Neuro Toxin is offline
_GamerX
AlliedModders Donor
Join Date: Jun 2011
Location: Fun Server
Old 10-01-2018 , 04:14   Re: Sort Array but preserve index
Reply With Quote #7

idk how index block in TopPlayers.Get(count, kv.GetNum("xp", 0));

and how to index block for sort in SortADTArray(TopPlayers, Sort_Descending, Sort_Integer);
__________________

Last edited by _GamerX; 10-01-2018 at 04:29.
_GamerX is offline
Send a message via ICQ to _GamerX Send a message via Skype™ to _GamerX
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 10-01-2018 , 05:47   Re: Sort Array but preserve index
Reply With Quote #8

An example of using the blocksize value for an ArrayList to store 2 values:
PHP Code:
ArrayList array = new ArrayList); // the number provided is the block size, ie. how many values to have per array element
array.Resize);
array.
Set01); // write value of 1 to first block in first index
array.Set02); // write value of 2 to second block in first index
int value1 = array.Get0); // get value in first block
int value2 = array.Get0); // get value in second block 
Then use SortADTArrayCustom to sort by whichever value in whichever block you want.

Last edited by hmmmmm; 10-01-2018 at 05:48.
hmmmmm is offline
_GamerX
AlliedModders Donor
Join Date: Jun 2011
Location: Fun Server
Old 10-01-2018 , 06:18   Re: Sort Array but preserve index
Reply With Quote #9

Thank you so much, I did not know there was a function Resize
__________________
_GamerX is offline
Send a message via ICQ to _GamerX Send a message via Skype™ to _GamerX
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 12-05-2018 , 13:44   Re: Sort Array but preserve index
Reply With Quote #10

I'll just leave here a demo if nobody objects.

Code:
	ArrayList al = CreateArray(ByteCountToCells(8)); // float + int ( sizeof(float) == 4, sizeof(int) == 4) )
	
	PrintToServer("blocks = %i", ByteCountToCells(8)); // 4 bytes per block. 8/4 = 2 (total blocks)
	
	float f[6] = { 1.0, 5.0, -99.0, 11.0, 999.0, -4.0 };
	int c[6] = { 1, 2, 3, 4, 5, 6 };
	
	for (int i = 0; i < 6; i++) {
		al.Push(f[i]); // set float ( sort key ) with increasing array size. Though, Resize() method, will be faster if we know total size in advance.
		al.Set(al.Length - 1, c[i], 1); // set int (index to track like another arrays) in the last ADT array index in the block # 1 (second block)
	}
	
	/* Using resize
	al.Resize(6);
	
	for (int i = 0; i < 6; i++) {
		al.Set(i, f[i], 0);
		al.Set(i, c[i], 1);
	}
	*/

	SortADTArray(al, Sort_Descending, Sort_Float);
	
	for (int i = 0; i < 6; i++)
		PrintToServer("%f = %i", al.Get(i, 0), al.Get(i, 1));
Quote:
blocks = 2
999.000000 = 5
11.000000 = 4
5.000000 = 2
1.000000 = 1
-4.000000 = 6
-99.000000 = 3
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]

Last edited by Dragokas; 12-05-2018 at 13:55.
Dragokas 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 23:51.


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