这是一个例子:
// 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'];
Run Code Online (Sandbox Code Playgroud)
我之前使用过带有函数的.sort,但是我仍然处于亏损状态.
UPDATE
看到一些答案后,我无法相信这对我来说并不明显.因此,有很多方法可以做到这一点,胜者将由jsperf决定.
(我也在向所有人提出工作答案)