Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by GameGuy · Mar 13, 2011 at 04:14 PM · arrayindexcomparesorthold

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Peter G · Mar 13, 2011 at 04:32 PM 0
Share

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

avatar image GameGuy · Mar 13, 2011 at 10:23 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Peter G · Mar 24, 2011 at 09:02 PM 0
Share

Why did I get voted down?

avatar image GameGuy · Mar 26, 2011 at 01:10 PM 0
Share

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

avatar image
1

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&lt;int, string&gt;(10, "ten"));
     kvplist.Add(new KeyValuePair&lt;int, string&gt;(9, "nine"));
     kvplist.Add(new KeyValuePair&lt;int, string&gt;(12, "twelve"));

     kvplist.Sort((KeyValuePair&lt;int, string&gt; kvp, KeyValuePair&lt;int, string&gt; kvp2) =&gt;
         {
             return kvp.Key.CompareTo(kvp2.Key);
         });

     foreach (KeyValuePair&lt;int, string&gt; kvp in kvplist)
     {
         Console.WriteLine(kvp.Value + ":" +kvp.Key);
     }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by raul corrales · Mar 24, 2011 at 05:58 PM

use this: link text

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

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

Trying to sort an array. What's wrong here? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges