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

sort array of objects in descending order javascript
javascript sort
javascript sort array of objects by key
javascript sort array of objects by key value
sort array of numbers in descending order javascript
javascript sort descending number
javascript sort date ascending and descending
sort string with numbers javascript

W3CSchools has this example:

var fruits = ["Banana", "Orange", "Apple", "Mango"];

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


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)?

If you consider



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


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 than obj.sort( function ) (except on chrome), and obj.sort( function ) (using 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 =;
  let t1 =;

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

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

  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]));

SQL ORDER BY Keyword, How do I sort a string by a number in JavaScript? Sorting data means arranging it in a certain order, often in an array-like data structure. You can use various ordering criteria, common ones being sorting numbers from least to greatest or vice-versa, or sorting strings lexicographically. You can even define your own criteria, and we'll go into practical ways of doing that by the end of this

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"];


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

JavaScript Array sort() Method, , "25" is bigger than "100", because "2" is bigger than "1". I don't know if TypedArray .sort() and then .reverse is faster than using a custom comparison function to sort in descending order. Or if we can copy-and-reverse on the fly with an iterator. Or if we can copy-and-reverse on the fly with an iterator.

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



Javascript Array.sort implementation?, . However, In Chrome's v8 source code, as of today, it uses QuickSort and InsertionSort, for smaller arrays. The ECMAscript standard does not specify which sort algorithm is to be used. Sort an array of strings according to string lengths; Sorting array of strings (or words) using Trie; Sort string of characters; Check if sum of Fibonacci elements in an Array is a Fibonacci number or not; Count of subarrays having exactly K perfect square numbers; Check if given words are present in a string; Kth most frequent Character in a

JavaScript Array sort: Sorting an Array More Effectively, By default, the sort() method sorts the array elements in ascending order with the The sort() method casts elements to strings and compares the strings to  Of course, these are not the only solutions but are the easiest and most straightforward answers. 1. Using sort () and reverse () function for ascending and descending sorting respectively. var a = prompt ('Enter a number');

Different ways to sort an array in descending order in C# , JavaScript Array sort() Method​​ The sort() method sorts the items of an array. The sort order can be either alphabetic or numeric, and either ascending (up) or descending (down). By default, the sort() method sorts the values as strings in alphabetical and ascending order. myList.sort(function(x, y){ return x.timestamp - y.timestamp; }) myList is a JavaScript array, which supports the sort method. This method accepts a function as argument, which sorts the array according to the returned value. Currently, the sort algorithm will place the element with the lowest timestamp first.

Sorting Strings using Bubble Sort, How to Pad an Integer Number With Leading Zeroes in C#?. More related articles in C#. File.WriteAllText(String  numArray.sort((a, b) => a - b); // For ascending sort numArray.sort((a, b) => b - a); // For descending sort Documentation: Mozilla Array.prototype.sort() recommends this compare function for arrays that don't contain Infinity or NaN.