The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

// Functionless sort()

// Arrow function sort((a, b) => { /* ... */ } )
// a is the first element for comparison and b is the second element for 
// comparison.

Parameters in the sort() method specify a function that defines the sort order. If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value.

Note that the array is sorted in place, and no copy is made.

The answer you need might be the answer given below or nearly corresponds to it.

The sort method can be conveniently used with function expressions:

const numbers = [1, 2, 3];
numbers.sort(function(a, b) {
  return b - a;
});
console.log(numbers);

// [3, 2, 1]

This perfectly reverses an array with any count of numbers you have.

For reference, you can read sort() method documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#:~:text=The%20sort%20method%20can%20be%20conveniently%20used%20with%20function%20expressions%3A

Answer from Sharjeel Faiq on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › sort-an-array-in-reverse-order-javascript
JavaScript - Sort an Array in Reverse Order in JS - GeeksforGeeks
July 23, 2025 - const arr = [ 10, 20, 25, 100 , 40] // Pass the comparator to to sort in reverse order arr.sort((a,b) => b - a) console.log(arr) Output · [ 100, 40, 25, 20, 10 ] Please go through this How to Sort Numeric Array using JavaScript?, to know how ...
🌐
W3Schools
w3schools.com › jsref › jsref_reverse.asp
JavaScript Array reverse() Method
❮ Previous JavaScript Array Reference Next ❯ · const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.reverse(); Try it Yourself » · The reverse() method reverses the order of the elements in an array.
Discussions

javascript - JS .reverse array using .sort - a trick which doesn't seem to work? - Stack Overflow
Why doesn't this code reverse an array? ... Explanation: The .sort method accepts a function that normally takes 2 arguments, in this case we don't care about them and use 'x'. That function compares the 2 elements and returns a negative number if they are in correct order, zero if they are ... More on stackoverflow.com
🌐 stackoverflow.com
Sorting strings in descending order in Javascript (Most efficiently)? - Stack Overflow
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). More on stackoverflow.com
🌐 stackoverflow.com
How can I reverse an array in JavaScript without using libraries? - Stack Overflow
I am saving some data in order using arrays, and I want to add a function that the user can reverse the list. I can't think of any possible method, so if anybody knows how, please help. More on stackoverflow.com
🌐 stackoverflow.com
Why does sort and reverse an array in Javascript give the desired output - Stack Overflow
The sort() method sorts the elements ... sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values. ... Launching the CI/CD and R Collectives and community editing features for... ... 3189 Why does my JavaScript code receive ... More on stackoverflow.com
🌐 stackoverflow.com
September 8, 2021
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › reverse
Array.prototype.reverse() - JavaScript - MDN Web Docs
In other words, elements order in the array will be turned towards the direction opposite to that previously stated. To reverse the elements in an array without mutating the original array, use toReversed().
🌐
GeeksforGeeks
geeksforgeeks.org › sort-an-array-in-reverse-order-javascript
JavaScript – Sort an Array in Reverse Order in JS | GeeksforGeeks
November 15, 2024 - Using array.sort() Method To sort an array in JavaScript, we can use array.sort() method. This method sorts the elements alphabetically in ascending order.JavaScriptlet arr = ["JS", "HTML", "CSS"]; arr.sort() console.log(arr);Output[ 'CSS', ...
🌐
CoreUI
coreui.io › answers › how-to-reverse-an-array-in-javascript
How to reverse an array in JavaScript · CoreUI
September 20, 2025 - In this example, numbers.reverse() changes the array from [1, 2, 3, 4, 5] to [5, 4, 3, 2, 1], with the last element becoming the first and vice versa. The method doesn’t create a new array but modifies the existing one, which makes it memory efficient but means the original order is lost. This is the same approach we use in CoreUI components for creating reverse-ordered lists and implementing chronological data display. To avoid mutating the original array, create a copy first: [...array].reverse(). For complex sorting needs, combine with sort(): array.sort().reverse() gives you descending alphabetical order...
Top answer
1 of 2
1

The sort() method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

// Functionless sort()

// Arrow function sort((a, b) => { /* ... */ } )
// a is the first element for comparison and b is the second element for 
// comparison.

Parameters in the sort() method specify a function that defines the sort order. If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value.

Note that the array is sorted in place, and no copy is made.

The answer you need might be the answer given below or nearly corresponds to it.

The sort method can be conveniently used with function expressions:

const numbers = [1, 2, 3];
numbers.sort(function(a, b) {
  return b - a;
});
console.log(numbers);

// [3, 2, 1]

This perfectly reverses an array with any count of numbers you have.

For reference, you can read sort() method documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#:~:text=The%20sort%20method%20can%20be%20conveniently%20used%20with%20function%20expressions%3A

2 of 2
0

The comparison function will swap the elements if the comparison function returns negative . so, just change the ` const trickReverse = arr => arr.sort(e => -1);

console.log(trickReverse([1, 2, 3])); -> [ 3,2,1] ` This will do the trick

For reference

🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Array › reverse
JavaScript Array reverse() - Reverse Array Order | Vultr Docs
September 27, 2024 - Initialize an array with string elements. Use the reverse() method. ... This outputs the array names in reverse order: ["David", "Carol", "Bob", "Alice"]. Combine reverse() with other array methods like sort().
Find elsewhere
Top answer
1 of 5
239

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

2 of 5
9

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.

🌐
W3Schools
w3schools.com › jsref › jsref_sort.asp
JavaScript Array sort() Method
// Create an Array const fruits = ["Banana", "Orange", "Apple", "Mango"]; // Sort the Array fruits.sort(); // Reverse the array fruits.reverse(); Try it Yourself »
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-sort-array-of-strings-descending
Sort an Array of Strings in Descending order in JavaScript | bobbyhadz
March 6, 2024 - ... Copied!// ✅ Sort in Descending order const strArr1 = ['a', 'c', 'z', 'f']; const descArr = strArr1.sort().reverse(); console.log(descArr); // 👉️ ['z', 'f', 'c', 'a'] When called without any parameters, the sort method converts the ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › sort
Array.prototype.sort() - JavaScript - MDN Web Docs
The sort() method of Array instances sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code unit values.
🌐
Matt Doyle
elated.com › home › blog › sorting javascript arrays
Sorting JavaScript Arrays - Matt Doyle | Elated Communications
July 23, 2022 - The reverse() method works much as you’d expect — it reverses the order of the elements in an array, so that the first element becomes the last, and the last becomes the first. Its syntax is simply: ... Note that it reverses the array itself, rather than returning a reversed copy of the array.
🌐
Career Karma
careerkarma.com › blog › javascript › javascript sort array: a how-to guide
JavaScript Sort Array: A How-To Guide | Career Karma
December 1, 2023 - There is also a built-in JavaScript function that allows you to sort an array in reverse order: reverse(). The reverse() method reverses an array so that the first element becomes the last, and the last element becomes the first.
🌐
C# Corner
c-sharpcorner.com › blogs › sorting-reversing-an-array-in-javascript
How to Sort an Array in JavaScript
March 15, 2023 - By default, the sort() method sorts the values as strings in alphabetical and ascending order. ... The reverse() method of the array in JavaScript reverses the elements in an array. ... Copy the HTML code and ...
🌐
W3Schools
w3schools.com › js › tryit.asp
JavaScript Arrays
The W3Schools online code editor allows you to edit code and view the result in your browser
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › toReversed
Array.prototype.toReversed() - JavaScript - MDN Web Docs
July 10, 2025 - The toReversed() method transposes the elements of the calling array object in reverse order and returns a new array.
🌐
SamanthaMing
samanthaming.com › tidbits › 74-how-to-reverse-an-array
How to reverse an array in JavaScript? | SamanthaMing.com
const originalArray = ['a', 'b', 'c']; const newArray = originalArray.reverse(); console.log(originalArray); // [ 'c', 'b', 'a' ] console.log(newArray); // [ 'c', 'b', 'a' ] Here are some recipes that won't mutate the original array.
🌐
GeeksforGeeks
geeksforgeeks.org › reverse-an-array-in-javascript
Reverse an Array in JavaScript | GeeksforGeeks
January 20, 2025 - Given there are two sorted arrays, we need to merge them into a single sorted array in JavaScript. We will see the code for each approach along with the output. Below are the possible approaches t ... Inversion Count for an array indicates - how far (or close) the array is from being sorted. If the array is already sorted, then the inversion count is 0, but if the array is sorted in the reverse order, the inversion count is the maximum. Formally speaking, two elements a[i] and a[j] form an invers