- Home /
Matching Index of two Arrays after one Array is sort
I have two Arrays with the same lenght(e.g. 3), one Array is a String Array and the other is an int Array. The int Array is sorted and gets a new index starting with the lowest value, how can I get the old index back(without destroying the new index) and compare it with the index of the string Array?
var stringID_1 = PlayerPrefs.GetString("String_1") // same for String_2 and _3
var intID_1 = PlayerPrefs.GetInt("Int_1") // same for Int2 and _3
//
function Update()
{
var stringArray = new Array(
stringID_1,
stringID_2,
stringID_3
);
Debug.Log(stringArray);
//
var intArray = new Array(
intID_1,
intID_2,
intID_3
);
Debug.Log(intArray);
//
intArray.Sort();
Debug.Log(intArray)
//
var newIndex = new Array(intArray);
//
Debug.Log("HighestInt" +newIndex[2]);
Debug.Log("LowestInt" +newIndex[0]);
//
//Here I want to combine for e.g intArray[1] and stringArray[1] ,
//but the Index for the intArray has changed after
//var newIndex = new Array(intArray);
//
Edit: Changed the Question title, it was misleading.
In general I want to print stringID_1 + intID_1 , but not all IDs are needed, only the Highest intID and the lowest this is why Iam sorting the intArray and pass it to a new Array. But after sorting the Indexes of the string_IDs and intID`s are not longer matching
Are you trying to assign an int to a string and vice-versa? In that case, then you should use a hashtable or dictionary.
Looks like a Hashtable is the the right way, I searched here, the script reference and in the wiki but I cant find the information how to sort a Hashtable. Sort is not a member of Hashtable, and passing the Hashtable to an Array and sort that Array is not working.
Answer by Peter G · Mar 14, 2011 at 12:07 AM
Use a SortedDictionary. This is basically what Niklas did, but with a different class.
Dont know. It took some time for me to understand & use Hashtables!
+1 for pointing in the right direction.
Answer by Niklas · Mar 13, 2011 at 11:42 PM
You could use a dictionary instead if you can live with only one of each ID. If not, you could use a list of KeyValuePair instead. This is C# and I'm not sure how this would look in js.
List<KeyValuePair<int, string>> kvplist = new List<KeyValuePair<int, string>>();
kvplist.Add(new KeyValuePair<int, string>(10, "ten"));
kvplist.Add(new KeyValuePair<int, string>(9, "nine"));
kvplist.Add(new KeyValuePair<int, string>(12, "twelve"));
kvplist.Sort((KeyValuePair<int, string> kvp, KeyValuePair<int, string> kvp2) =>
{
return kvp.Key.CompareTo(kvp2.Key);
});
foreach (KeyValuePair<int, string> kvp in kvplist)
{
Console.WriteLine(kvp.Value + ":" +kvp.Key);
}
Your answer
Follow this Question
Related Questions
Sort Multiple Arrays By Their Length 1 Answer
Change an objects Array position dynamiclly in a match 3 game 0 Answers
Sorting a list by distance to an object? 1 Answer
Sorting array 0 Answers