Here is an example:
// 0 1 2 3 4
var people = ['jack','jill','nancy','tom','cartman'];
var order = [3,1,4,0,2];
// somehow sort people array to the order specified in the order array
// 3 1 4 0 2
people == ['tom','jill','cartman','jack','nancy'];
I have used .sort with a function before, but am still at a loss on this one.
UPDATE
after seeing some answers, I can’t believe this was not obvious to me. So as there are many ways to do this, winner will be determined by jsperf.
(also I am upvoting everyone with a working answer)
orderis an array of indicies. So just iterate through that, pulling out the values you want in the order spcified, making a new array.Sometimes sorting isn’t “sorting”. Sometimes you just need to make a new thing by pulling data from your other things.