I have two arrays. I should do a normal sort on one (descending or ascending) and sort the other according to how the first array is sorted. This is because each element in the first array has a relationship with the same index element on the second array, and I must keep this relationship true. For example:
sortThis=[3,1,2];
sortAccording=["With 3","With 1","With 2];
I couldn’t find any way to take the index changes from JavaScript’s sort function.
Solution:
To achieve that, you have to zip both arrays in just one. This is, given you have this two array:
After zip them, you will have the following array:
Then, you sort it by a in order to have:
What next?
Well, once you have this array sorted by what you want, you have to extract their values with the map function and finnaly you will have your two arrays sorted by the same criteria:
The code:
You also can see it working here: http://jsfiddle.net/lontivero/cfpcJ/
Good luck!