-
I have 2 arrays. One containing numbers and one containing strings. I am sorting the array with numbers (high to low). This is working fine. But, I want to sort the array containing strings so that it ends up in the same order as the numbers array. For example, if after the sort, number[2] moves to number[5], I want string[2] to move to string[5] also, and so forth with the rest of the value in the arrays. Both arrays contain 9 values, if that matters....
This is the sort code I have been using to sort the array containing numbers:
On frame 1:
function compareResults (result01, result02) {
return result02 - result01;
}
On a button:
on (release) {
results.sort(compareResults);
}
-
half as fun, double the price
I would use a 2 dimensional array like so:
Code:
function DecendResults (result1, result2) {
return result1[0] < result2[0];
}
function MixResults () {
return Math.floor(Math.random()*2)*2-1;
}
myArray = [[0,"a"],[1,"b"],[2,"c"],[3,"d"],[4,"e"],[5,"f"],[6,"g"],[7,"h"],[8,"i"],[9,"j"]]
trace(myArray)
myArray.sort(MixResults)
trace(myArray)
myArray.sort(DecendResults)
trace(myArray)
first I randomly ordered the array I made to mix it up some (just for this example), then when using DecendResults I compare the first element of the contained arrays of each elements of the main array (the number portion is compared). Then the sort order function will sort the main array keeping the elements of the contained arrays consistent.
-
Wow! It worked perfectly! Thankyou so much!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|