可能重复:
JavaScript - Sort an array based on another array of integers
Javascript - sort array based on another array排序数组
如果我有一个这样的数组:
['one','four','two']
而另一个数组像这样:
[{
key: 'one'
},{
key: 'two'
},{
key: 'four'
}]
如何排序第二个数组,因此它的key
属性遵循第一个的顺序?在这种情况下,我想:
[{
key: 'one'
},{
key: 'four'
},{
key: 'two'
}]
可能重复 - 排序依据的另一个数组的数组整数](http://stackoverflow.com/questions/4046967/javascript-sort-an-array-based-on-another-array-of-integers)和/或[Javascript - 基于另一个数组的排序数组](h TTP://计算器。com/questions/13304543/javascript-sort-array-based-on-another-array?lq = 1) –
@TedHopp不完全,它解决了两个平面数组,但这种情况使用了对象键,这使得更难以有效排序除非你想在每次迭代中搜索第二个)。 – David
我期望这样的工作,但不知何故......它不。 : -/ 'a2.sort(function(a,b){a1 [a2.indexOf(a)] - a1 [a2.indexOf(b)]})' –