Raised This Month: $32 Target: $400
 8% 

Syncing two arrays with one array being sorted.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Drixevel
AlliedModders Donor
Join Date: Sep 2009
Location: Somewhere headbangin'
Old 08-14-2016 , 19:17   Syncing two arrays with one array being sorted.
Reply With Quote #1

So I'm syncing two arrays together with one actually being sorted and the other kind of just following suit. The goal is to make a local stats system where this would show the top results in order based on the statistical value of the 2nd array.

Example: (Works with the comments where they are)
Code:
#include <sourcemod>
#include <sdktools>

public void OnPluginStart()
{
	Handle hArray1 = CreateArray(ByteCountToCells(MAX_NAME_LENGTH));
	PushArrayString(hArray1, "Should be 8");
	//PushArrayString(hArray1, "Should be 0");
	PushArrayString(hArray1, "Should be 12");
	PushArrayString(hArray1, "Should be 6");
	PushArrayString(hArray1, "Should be 1");
	PushArrayString(hArray1, "Should be 0");
	PushArrayString(hArray1, "Should be 0");
	PushArrayString(hArray1, "Should be 0");

	Handle hArray2 = CreateArray();
	PushArrayCell(hArray2, 8);
	//PushArrayCell(hArray2, 0);
	PushArrayCell(hArray2, 12);
	PushArrayCell(hArray2, 6);
	PushArrayCell(hArray2, 1);
	PushArrayCell(hArray2, 0);
	PushArrayCell(hArray2, 0);
	PushArrayCell(hArray2, 0);
	
	SortADTArrayCustom(hArray2, OnSortFunction, hArray1);
	
	PrintToServer("Results:");
	for (new i = 0; i < GetArraySize(hArray2); i++)
	{
		char sName[MAX_NAME_LENGTH];
		GetArrayString(hArray1, i, sName, sizeof(sName));

		int iTokens = GetArrayCell(hArray2, i);
		PrintToServer("%s - %i", sName, iTokens);
	}
}


public int OnSortFunction(int index1, int index2, Handle array, Handle hndl)
{
	int iValue1 = GetArrayCell(array, index1);
	int iValue2 = GetArrayCell(array, index2);
	
	if (iValue1 > iValue2)
	{
		SwapArrayItems(hndl, index1, index2);
		return -1;
	}
	else if (iValue1 < iValue2)
	{
		return 1;
	}
	
	return 0;
}
I know I'm not that off because this works to an extent. That code works as long as the 2nd items for both arrays aren't pushed. Probably a math thing that I'm not very good at.

Any help would be appreciated, cheers.
Drixevel is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 08-14-2016 , 21:38   Re: Syncing two arrays with one array being sorted.
Reply With Quote #2

Calling SwapArrayItems() inside your comparison function isn't going to end up sorting your second array. You can't know exactly how the sorting algorithm is going to work, so you can't know if or where it will be moving elements, even if you know which elements its comparing. You can't know because the API does not describe or guarantee how the sorting will work.

One suggestion might be to rearrange your data to only use a single ADT array to avoid having to sync multiple arrays. A simple (but maybe not best) way to do this is to store both your numeric and string values in a trie or datapack and put those into your array.

Last edited by Fyren; 08-14-2016 at 21:39.
Fyren is offline
Drixevel
AlliedModders Donor
Join Date: Sep 2009
Location: Somewhere headbangin'
Old 08-15-2016 , 01:03   Re: Syncing two arrays with one array being sorted.
Reply With Quote #3

Quote:
Originally Posted by Fyren View Post
Calling SwapArrayItems() inside your comparison function isn't going to end up sorting your second array. You can't know exactly how the sorting algorithm is going to work, so you can't know if or where it will be moving elements, even if you know which elements its comparing. You can't know because the API does not describe or guarantee how the sorting will work.

One suggestion might be to rearrange your data to only use a single ADT array to avoid having to sync multiple arrays. A simple (but maybe not best) way to do this is to store both your numeric and string values in a trie or datapack and put those into your array.
I figured my method was a bit insane but I felt like I was on the right path somewhere because the names and the stats were sync'd and properly sorted without issue until 0's were introduced least from testing.

Thanks for the info.
Drixevel is offline
Reply


Thread Tools
Display Modes

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 00:11.


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