admin管理员组文章数量:1134236
W3CSchools has this example:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
fruits.reverse();
Is this the most efficient way to sort strings in descending order in 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)?
W3CSchools has this example:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
fruits.reverse();
Is this the most efficient way to sort strings in descending order in 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)?
5 Answers
Reset to default 234If you consider
obj.sort().reverse();
VS
obj.sort((a, b) => (a > b ? -1 : 1))
VS
obj.sort((a, b) => b.localeCompare(a) )
The performance winner is : obj.sort().reverse()
.
Testing with an array of 10.000 elements,
obj.sort().reverse()
is faster thanobj.sort( function )
(except on chrome), andobj.sort( function )
(usinglocalCompare
).
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]));
Using just sort
and reverse
a
> Z
, that is wrong if you want to order lower cases and upper cases strings:
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)
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.
The easiest way to revers the order of sorting is by swapping the operands. In ES2015 that's as easy as [b, a] = [a, b]
. A full example:
function compareWithOrder(a, b, shouldReverse = false) {
if (shouldReverse) {
[b, a] = [a, b]
}
return yourComparatorFn(a, b)
}
var arr = ["a","b","c","A","B","Z"];
arr.sort((a,b)=>b.localeCompare(a))
console.log(arr)
I know this is an old question, but an interesting one. This is my solution for a non-special character's input.
var arr = ["a","b","c","A","B","Z"];
console.log(arr.sort((a,b)=> {
const lastCodeIn = b.toLowerCase().charCodeAt();
const lastCode = b.charCodeAt();
const firstCodeIn = a.toLowerCase().charCodeAt();
const firstCode = a.charCodeAt();
if(lastCodeIn - firstCodeIn === 0){
return lastCode - firstCode;
}
return lastCodeIn - firstCodeIn;
})
);//[ 'Z', 'c', 'b', 'B', 'a', 'A' ]
The reason is that ascii code for UPPER case are lower than lower case.
本文标签: Sorting strings in descending order in Javascript (Most efficiently)Stack Overflow
版权声明:本文标题:Sorting strings in descending order in Javascript (Most efficiently)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736829220a1954628.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
.sort()
and.reverse()
is already the most efficient way. – Sebastian Simon Commented Aug 26, 2018 at 20:52.sort((a, b) => -(a>b)||+(a<b))
– Bergi Commented Aug 26, 2018 at 20:53reverse()
doesn't care about the locales, it only modifies the indexes of the array in reverse order – colxi Commented Aug 26, 2018 at 21:25