You can pass a custom comparison function to Array.prototype.sort(), like so:

var sortedArray = array.sort(function(a, b) { return a - b; });

This would sort an array of integers in ascending order. The comparison function should return:

  • an integer that is less than 0 if you want a to appear before b
  • an integer that is greater than 0 if you want b to appear before a
  • 0 if a and b are the same

So, for this example you would want something like:

var sortedArray = array.sort(function(a, b) {
  return b[0] - a[0];
});

If you wanted to sort on both elements of each sub-array (ie. sort by the first element descending, then if they are the same then sort by the second element descending), you could do this:

var sortedArray = array.sort(function(a, b) {
  if (a[0] == b[0]) {
    return a[1] - b[1];
  }
  return b[0] - a[0];
});

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort for more info.

Answer from Gordon Larrigan on Stack Overflow
🌐
W3Schools
w3schools.com › js › js_array_sort.asp
JavaScript Array Sort
ES2023 added the toSorted() method as a safe way to sort an array without altering the original array.
🌐
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.
Discussions

javascript - How can I sort an array of integers? - Stack Overflow
Mozilla Array.prototype.sort() recommends this compare function for arrays that don't contain Infinity or NaN. (Because Infinity - Infinity is NaN, not 0). Also examples of sorting objects by key. ... Nice. But is there really no out-of-the-box way to get a numerical sort from javascript? More on stackoverflow.com
🌐 stackoverflow.com
javascript - Sorting an array of objects by property values - Stack Overflow
While it is a bit of an overkill for just sorting a single array, this prototype function allows to sort Javascript arrays by any key, in ascending or descending order, including nested keys, using dot syntax. More on stackoverflow.com
🌐 stackoverflow.com
Just an example of string sorting because I couldn't find any other.
For Unicode no explicit support beyond the implicit utf8 stuff is in there. See https://zig.news/dude_the_builder/unicode-string-operations-536e for a discussion. More on reddit.com
🌐 r/Zig
7
13
November 20, 2023
Help sorting an array of objects
Hi, can you put this in a codepen ? I find it hard to figure out what you really want to do. When I read your code, I understand that you want to change the content of the first anchor element of each list sharing the same ancestor than your "specificFilter" so that this content is sorted alphabetically... This looks weird so to me, so is that really what you want to do ? More on reddit.com
🌐 r/learnjavascript
19
1
July 17, 2023
🌐
W3Schools
w3schools.com › jsref › jsref_sort.asp
JavaScript Array sort() Method
❮ Previous JavaScript Array Reference ... Try it Yourself » · More Examples Below ! Share Link Copied · The sort() method sorts the elements of an array....
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › sort-an-array-of-arrays-in-javascript
Sort An Array Of Arrays In JavaScript - GeeksforGeeks
July 23, 2025 - To sort an array of arrays by a specific column, you need to provide a custom comparator function to sort() that specifies which index in the sub-arrays should be compared.
🌐
LogRocket
blog.logrocket.com › home › javascript’s sort()method: from basics to custom sorting
JavaScript's sort()method: From basics to custom sorting - LogRocket Blog
March 7, 2025 - To recap the previous section, the JavaScript sort() array method takes an optional compare function as a callback. If you don’t pass a callback, the array’s elements will be coerced to strings and are sorted in ascending order of the numerical values of their UTF-16 code units.
🌐
Codingem
codingem.com › home › javascript how to sort an array of arrays (quick guide + deetails)
JavaScript How to Sort an Array of Arrays (Quick Guide + Deetails)
July 10, 2025 - To sort an array of arrays in JavaScript, pass a custom comparison function into the sort() function call.
Find elsewhere
🌐
Mimo
mimo.org › glossary › javascript › sort
JavaScript Sort Method: Advanced Array Sorting Techniques
The sort() method sorts the elements of an array in-place and and returns the reference to the same array.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-array-sort-method
JavaScript Array sort() Method - GeeksforGeeks
The sort() method in JavaScript is used to arrange array elements in a specific order, usually alphabetically or in ascending order. It modifies the original array and returns the sorted result.
Published: January 16, 2026
🌐
SitePoint
sitepoint.com › blog › javascript › quick tip: how to sort an array of objects in javascript
Sort an Array of Objects in JavaScript with the sort() method— SitePoint
November 7, 2024 - JavaScript’s native Array.sort function can be used to sort an array of objects, utilizing a compare function to define the sorting logic for strings, numbers, dates, and other properties.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript array methods › array.prototype.some()
JavaScript Array sort: Sorting an Array More Effectively
November 8, 2024 - The output shows that the sort() method evaluates each element multiple times e.g., Amazon 4 times, Congo 2 times. If the number of array elements is increasing, it will impact the performance.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-sort-javascript-array-accurately
JavaScript Sort Array - How to Sort an Array Accurately
November 7, 2024 - The sort() method can be used to sort elements of an array in ascending order based on Unicode character code values by default.
🌐
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 - You can use the JavaScript sort() method to sort an array. The sort() method accepts an array as an argument and sorts its values in ascending order. Arrays are sorted in place which means the original array is modified.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › toSorted
Array.prototype.toSorted() - JavaScript - MDN Web Docs
The toSorted() method of Array instances is the copying version of the sort() method. It returns a new array with the elements sorted in ascending order.
🌐
Scaler
scaler.com › home › topics › javascript › array sort in javascript
Array Sort in JavaScript - Scaler Topics
May 15, 2022 - Now that we've created the array and displayed its elements let's sort the array such that the multiples of 5 are placed in increasing order. ... The arr array can be sorted using array sort. We need to call the sort() method with arr using dot (.) operator. The sort array javascript method ...
🌐
Programiz
programiz.com › javascript › library › array › sort
JavaScript Array sort()
Online Python Online JavaScript ... Online Go Online Rust Online Scala Online Dart Online R Online Ruby ... The sort() method sorts the items of an array in a specific order (ascending or descending)....
🌐
SheCodes
shecodes.io › athena › 42896-how-to-sort-a-javascript-array
[JavaScript] - How to sort a JavaScript array - SheCodes | SheCodes
Learn how to sort a JavaScript array using the `sort()` method with a compare function for sorting numbers in ascending or descending order.
🌐
Medium
noncodersuccess.medium.com › sorting-arrays-like-a-pro-advanced-techniques-in-javascript-e3590b75d591
Sorting Arrays Like a Pro: Advanced Techniques in JavaScript | by NonCoderSuccess | Medium
April 14, 2024 - Even if objects have properties of different data types, the sort() method can be used to sort the array. The solution is to write a compare function to compare the property values: Sorting arrays in JavaScript might seem like a small thing, ...