Javascript 在Javascript中按降序对字符串进行排序(最有效)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52030110/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:55:23  来源:igfitidea点击:

Sorting strings in descending order in Javascript (Most efficiently)?

javascriptsorting

提问by Ole

W3CSchools has this example:

W3CSchools 有这个例子:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
fruits.reverse();

Is this the most efficient way to sort strings in descending order in Javascript?

这是在 Javascript 中按降序对字符串进行排序的最有效方法吗?

Update

更新

One of the answers is using localeCompare. Just curious whether if we do reverse(), will that work for all locales (Maybe this is a separate question - Just let me know in the comments)?

答案之一是使用localeCompare. 只是好奇如果我们这样做reverse(),是否适用于所有语言环境(也许这是一个单独的问题 - 请在评论中告诉我)?

回答by colxi

If you consider

如果你考虑

obj.sort().reverse();

VS

VS

obj.sort((a, b) => (a > b ? -1 : 1))

VS

VS

obj.sort((a, b) => b.localeCompare(a) )

The performance winner is : obj.sort().reverse().

性能优胜者是:obj.sort().reverse()

Testing with an array of 10.000 elements, obj.sort().reverse()is faster than obj.sort( function )(except on chrome), and obj.sort( function )(using localCompare).

使用 10.000 个元素的数组进行测试, obj.sort().reverse()obj.sort( function )(chrome 除外)和obj.sort( function )(使用localCompare)快。

Performance test here :

性能测试在这里:

var results = [[],[],[]]

for(let i = 0; i < 100; i++){
  const randomArrayGen = () => Array.from({length: 10000}, () => Math.random().toString(30));
  const randomArray = randomArrayGen();
  const copyArray = x => x.slice();

  obj = copyArray(randomArray);
  let t0 = performance.now();
  obj.sort().reverse();
  let t1 = performance.now();

  obj = copyArray(randomArray);
  let t2 = performance.now();
  obj.sort((a, b) => (a > b ? -1 : 1))
  let t3 = performance.now();

  obj = copyArray(randomArray);
  let t4 = performance.now();
  obj.sort((a, b) => b.localeCompare(a))
  let t5 = performance.now();  

  results[0].push(t1 - t0);
  results[1].push(t3 - t2);
  results[2].push(t5 - t4);  
}

const calculateAverage = x => x.reduce((a,b) => a + b) / x.length ;

console.log("obj.sort().reverse():                   " + calculateAverage(results[0]));
console.log("obj.sort((a, b) => (a > b ? -1 : 1)):   " + calculateAverage(results[1]));
console.log("obj.sort((a, b) => b.localeCompare(a)): " + calculateAverage(results[2]));

回答by Emeeus

Using just sortand reversea> Z, that is wrong if you want to order lower cases and upper cases strings:

如果您想对小写和大写字符串进行排序,则仅使用sortreversea>Z是错误的:

var arr = ["a","b","c","A","B","Z"];

arr.sort().reverse();

console.log(arr)//<-- [ 'c', 'b', 'a', 'Z', 'B', 'A' ] wrong!!!

English characters

英文字符

var arr = ["a","b","c","A","B","Z"];

arr.sort((a,b)=>b.localeCompare(a))

console.log(arr)

Special characters using locales, in this example es (spanish)

使用locales 的特殊字符,在本例中为 es(西班牙语)

var arr = ["a", "á", "b","c","A","á","B","Z"];

arr.sort((a, b) => b.localeCompare(a, 'es', {sensitivity: 'base'}))


console.log(arr)

sensitivity in this case is base:

在这种情况下,敏感性是基础

Only strings that differ in base letters compare as unequal. Examples: a ≠ b, a = á, a = A.

只有基本字母不同的字符串比较为不相等。示例:a ≠ b,a = á,a = A。

回答by Mungai_Keren

var arr = ["a","b","c","A","B","Z"];

arr.sort((a,b)=>b.localeCompare(a))

console.log(arr)