x


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

more ▼

asked Mar 13 '11 at 04:14 PM

GameGuy gravatar image

GameGuy
610 22 28 40

Are you trying to assign an int to a string and vice-versa? In that case, then you should use a hashtable or dictionary.

Mar 13 '11 at 04:32 PM Peter G

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.

Mar 13 '11 at 10:23 PM GameGuy
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Use a SortedDictionary. This is basically what Niklas did, but with a different class.

more ▼

answered Mar 14 '11 at 12:07 AM

Peter G gravatar image

Peter G
15k 16 44 136

Why did I get voted down?

Mar 24 '11 at 09:02 PM Peter G

Dont know. It took some time for me to understand & use Hashtables!
+1 for pointing in the right direction.

Mar 26 '11 at 01:10 PM GameGuy
(comments are locked)
10|3000 characters needed characters left

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);
        }
more ▼

answered Mar 13 '11 at 11:42 PM

Niklas gravatar image

Niklas
179 7

(comments are locked)
10|3000 characters needed characters left

use this: link text

more ▼

answered Mar 24 '11 at 05:58 PM

raul corrales gravatar image

raul corrales
268 25 28 34

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1363
x65
x57
x52
x32

asked: Mar 13 '11 at 04:14 PM

Seen: 1470 times

Last Updated: Mar 13 '11 at 07:04 PM