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
0
Question by PProductions · Sep 09, 2012 at 07:58 PM · javascriptarraymultiplesort

Is it possible to sort arrays based upon a previous sort?

I have several arrays with differnt data inside of them. for example, firstname[], lastname[] etc. Could I run an Array.Sort() to put one of the arrays in a alphabetical order and then use that sorting data to apply to the other arrays thus keeping all of the data aligned? I am using Javascript. I am also trying to avoid too many 'for' loops if possible?

Comment
Add comment
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

2 Replies

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

Answer by Eric5h5 · Sep 09, 2012 at 08:34 PM

It's best not to use multiple parallel arrays; it's a much better idea to create a class that has firstname, lastname, etc. and make an array of that class. You can create a custom sort function based on whatever criteria.

 class NameData {
     var firstName : String;
     var lastName : String;
     function NameData (firstName : String, lastName : String) {
         this.firstName = firstName;
         this.lastName = lastName;
     }
 }
 
 function LastNameSort (a : NameData, b : NameData) {
     return System.String.Compare (a.lastName, b.lastName);
 }
 
 function Awake () {
     var folks = [NameData("John", "Smith"), NameData("Jane", "Doe")];
     System.Array.Sort (folks, LastNameSort);
     print (folks[0].firstName);
 }
Comment
Add comment · Show 3 · 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 PProductions · Sep 10, 2012 at 05:06 PM 0
Share

Thank you both for this. I don't know what I was thinking when I wrote this script :p I had already used classes somewhere else in the project but didn't think to use them here. Two quick questions: LastNameSort() has 2 parameters but I presume this sort function can handle any number for entries in the array?

Also do I declare my variable holder for the class as a new Array() then populate it with instances of this class?

thanks agian!

avatar image Eric5h5 · Sep 10, 2012 at 10:38 PM 0
Share

> LastNameSort() has 2 parameters but I presume this sort function can handle any number for entries in the array?

It's a sorting function used in Array.Sort, so it can only have two parameters. It's used for comparing two arbitrary entries in the array, and must return -1, 0, or 1 depending on whether the first entry is less than, equal to, or greater than the second entry.

> Also do I declare my variable holder for the class as a new Array() then populate it with instances of this class?

You would never use new Array(); it's slow and obsolete. Always use built-in arrays or generic Lists (or other collections, depending on the circumstances, such as Dictionary, Queue, etc.). $$anonymous$$y example uses a built-in array of NameData. It could have been done like this:

 var folks = new NameData[2];
 folks[0] = NameData("John", "Smith");
 folks[1] = NameData("Jane", "Doe");

But that's kinda tedious.

avatar image PProductions · Sep 11, 2012 at 07:17 AM 0
Share

Ok, thanks. i already have a function that populates my variables.

avatar image
0

Answer by whydoidoit · Sep 09, 2012 at 08:26 PM

I guess the first question is why are you using multiple arrays? You should have a class or a struct defined with firstname, lastname etc then there would be one array that you could sort and all of the data would be kept together.

If you insist on having multiple arrays then you need another array of ints which is the index into the other arrays - sort this using the lookup to lastname (or whatever) then use it to get to the entries in all of the arrays.

If you use Linq you can do both noraml and multikey sorting in Javascript.

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sorting Array 1 Answer

Sort Multiple Arrays By Their Length 1 Answer

Help with sorting values from a class 2 Answers

Need help using GUI.Button/ input storage 1 Answer

Convert Array into One String (Js) 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