Kind of hacks, but it works.
var A = [0.5, 0.6, 0.5, 0.7, 0.8, 0.1]; var B = ['a', 'b', 'c', 'd', 'e', 'f']; var all = []; for (var i = 0; i < B.length; i++) { all.push({ 'A': A[i], 'B': B[i] }); } all.sort(function(a, b) { return aA - bA; }); A = []; B = []; for (var i = 0; i < all.length; i++) { A.push(all[i].A); B.push(all[i].B); } console.log(A, B);
jsFiddle .
Exit
0.1, 0.5, 0.5, 0.6, 0.7, 0.8 ["f", "a", "c", "b", "d", "e"]
Basically, we create objects with a clear relationship between A and B in a new array, and then sort() ing.
Then I go back and restore the original of two arrays.
Update
Már Örlygsson gives a good conclusion in the comments. Instead of creating an object such as {A: 0.5, B: 'a'} , he suggests putting the values of A an B into arrays of the type [0.5, 'a'] .
This should be faster, although it will be slightly less readable if you need to debug the all array. I will leave this to you if you encounter performance problems, go through both of these methods and select the fastest.
alex Mar 25 '11 at 0:25 2011-03-25 00:25
source share