- Home /
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?
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);
}
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!
> 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.
Ok, thanks. i already have a function that populates my variables.
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.
Your answer
Follow this Question
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