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.