在这个例子中:
let a = ['New York', 'New Hampshire', 'Maryland'];
let collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'});
a.sort(collator.compare);如何按降序对此数组排序?
发布于 2017-09-23 07:57:28
您可以切换参数:
a.sort( (x, y) => collator.compare(y, x) )或者排序和反转:
a.sort(collator.compare).reverse()发布于 2017-09-23 07:41:39
最简单的方法是使用reverse
let a = ['New York', 'New Hampshire', 'Maryland'];
a.reverse();
console.log(a)
或者,您可以遍历a数组并将每一项unshift到新数组中:
let a = ['New York', 'New Hampshire', 'Maryland'];
let b = [];
a.forEach((item) => {
b.unshift(item);
})
console.log(b)
https://stackoverflow.com/questions/46374566
复制相似问题