Javascript javascript排序稀疏数组保留索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12137690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript sort sparse array keep indexes
提问by TestersGonnaTest
What is the best method to sort a sparse array and keep the elements on the same indexes? For example:
对稀疏数组进行排序并将元素保持在相同索引上的最佳方法是什么?例如:
a[0] = 3,
a[1] = 2,
a[2] = 6,
a[7] = 4,
a[8] = 5,
I would like after the sort to have
我希望排序后有
a[0] = 2,
a[1] = 3,
a[2] = 4,
a[7] = 5,
a[8] = 6.
回答by jfriend00
Here's one approach. It copies the defined array elements to a new array and saves their indexes. It sorts the new array and then puts the sorted results back into the indexes that were previously used.
这是一种方法。它将定义的数组元素复制到一个新数组并保存它们的索引。它对新数组进行排序,然后将排序后的结果放回到以前使用的索引中。
var a = [];
a[0] = 3;
a[1] = 2;
a[2] = 6;
a[7] = 4;
a[8] = 5;
// sortFn is optional array sort callback function,
// defaults to numeric sort if not passed
function sortSparseArray(arr, sortFn) {
var tempArr = [], indexes = [];
for (var i = 0; i < arr.length; i++) {
// find all array elements that are not undefined
if (arr[i] !== undefined) {
tempArr.push(arr[i]); // save value
indexes.push(i); // save index
}
}
// sort values (numeric sort by default)
if (!sortFn) {
sortFn = function(a,b) {
return(a - b);
}
}
tempArr.sort(sortFn);
// put sorted values back into the indexes in the original array that were used
for (var i = 0; i < indexes.length; i++) {
arr[indexes[i]] = tempArr[i];
}
return(arr);
}
Working demo: http://jsfiddle.net/jfriend00/3ank4/
回答by Oriol
You can
你可以
- Use
filter
orObject.values
to obtain an array with the values of your sparse array. - Then
sort
that array, from largest to smaller. Be aware it's not stable, which may be specially problematic if some values are not numeric. You can use your own sorting implementation. - Use
map
andpop
to obtain the desired array. Assign it toa
.
- 使用
filter
或Object.values
获取包含稀疏数组值的数组。 - 然后
sort
是那个数组,从大到小。请注意它不稳定,如果某些值不是数字,这可能会特别成问题。您可以使用自己的排序实现。 - 使用
map
和pop
获取所需的数组。将其分配给a
.
var b = a.filter(function(x) {
return true;
}).sort(function(x,y) {
return y - x;
});
a = a.map([].pop, b);
Or, in ECMAScript 2017,
或者,在 ECMAScript 2017 中,
a = a.map([].pop, Object.values(a).sort((x,y) => y-x));
回答by happierall
// Update for your needs ('position' to your key).
function updateIndexes( list ) {
list.sort( ( a, b ) => a.position - b.position )
list.forEach( ( _, index, arr ) => {
arr[ index ].position = index
} )
}
var myList = [
{ position: 8 },
{ position: 5 },
{ position: 1 },
{ position: 9 }
]
updateIndexes( myList )
// Result:
var myList = [
{ position: 1 },
{ position: 2 },
{ position: 3 },
{ position: 4 }
]
回答by user1597524
var arr = [1,2,3,4,5,6,7,8,9,10];
// functions sort
function sIncrease(i, ii) { // ascending
if (i > ii)
return 1;
else if (i < ii)
return -1;
else
return 0;
}
function sDecrease(i, ii) { //descending
if (i > ii)
return -1;
else if (i < ii)
return 1;
else
return 0;
}
function sRand() { // random
return Math.random() > 0.5 ? 1 : -1;
}
arr.sort(sIncrease); // return [1,2,3,4,5,6,7,8,9,10]
arr.sort(sDecrease); // return [10,9,8,7,6,5,4,3,2,1]
arr.sort(sRand); // return random array for examle [1,10,3,4,8,6,9,2,7,5]