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 used .sort with a function before, but I still don't get it.
UPDATE
having seen some answers, I cannot believe that this was not obvious to me. Since there are many ways to do this, the winner will be determined by jsperf.
(I also support everyone with a working answer)
THE RACE! http://jsperf.com/array-sorted-to-order-array3
source share