By default, the sort method sorts elements alphabetically. To sort numerically just add a new method which handles numeric sorts (sortNumber, shown below) -

var numArray = [140000, 104, 99];
numArray.sort(function(a, b) {
  return a - b;
});

console.log(numArray);

Documentation:

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.

Answer from aks on Stack Overflow
🌐
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 to sort numbers correctly with Array.sort()? - Stack Overflow
The default sort for arrays in Javascript is an alphabetical search. More on stackoverflow.com
🌐 stackoverflow.com
How do you sort numbers and number strings in Javascript? (From actual least to greatest
Have you read the documentation for sort()? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort sort(function compareFn(a, b) { /* … */ }) If compareFn is not supplied, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order. For example, "banana" comes before "cherry". In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in the Unicode order. All undefined elements are sorted to the end of the array. More on reddit.com
🌐 r/learnjavascript
8
1
September 6, 2022
JavaScript's `Array.sort()` converts everything to strings by default!
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code unit values. To sort the elements in an array without mutating the original array, use toSorted(). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSorted The real life pro tip here is to ALWAYS read documentation for methods you plan to use at least once. More on reddit.com
🌐 r/learnprogramming
2
0
October 25, 2025
array.sort() Does Not Simply Sort Numbers in JavaScript

Your Point 2 is valuable. Most sorting tutorials I see jump immediately to .sort((a, b) => a - b) for sorting numbers, without explaining how we get there.

You start with a description of what a return value of a positive number, negative number, or zero means, show an example of how we'd write out a longform sort function, and then lead to the shortcut, demonstrating that it will return a positive number, negative number, or zero.

More on reddit.com
🌐 r/learnjavascript
1
1
January 27, 2021
🌐
W3Schools
w3schools.com › jsref › jsref_sort.asp
JavaScript Array sort() Method
The sort() method sorts the elements of an array. The sort() method sorts the elements as strings in alphabetical and ascending order. The sort() method overwrites the original array.
🌐
DigitalOcean
digitalocean.com › community › tutorials › js-array-sort-numbers
Using JavaScript's sort Method for Sorting Arrays of Numbers | DigitalOcean
It can be tricky at first to understand the rules for the array sort method in JavaScript. This post should help with that!
🌐
freeCodeCamp
freecodecamp.org › news › how-to-sort-javascript-array-accurately
JavaScript Sort Array - How to Sort an Array Accurately
November 7, 2024 - For sorting an array of numbers, the comparison function should subtract the second number from the first number.
🌐
DEV Community
dev.to › elmarshall › sorting-an-array-of-number-strings-in-javascript-la7
Sorting an Array of Number Strings in JavaScript - DEV Community
May 4, 2020 - I didn't realize this is a case of another built in method for JavaScript. The function is actually an optional parameter, called the compareFunction, that allows you to define an alternative sort order. ... When the sort() method compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value. The magic is that by performing an operation (subtraction) on the numbers in question, JavaScript converts them to integers.
🌐
Raddevon
raddevon.com › articles › sort-array-numbers-javascript
How do I sort an array of numbers in Javascript? » Rad Devon
In order to sort numbers, you’ll need to write a function that returns a negative number if a is less than b, returns a positive number if b is less than a, and returns 0 if the numbers are the same.
Find elsewhere
🌐
Medium
medium.com › @us.panliu › how-to-use-the-javascript-array-sort-method-to-sort-arrays-of-numbers-strings-and-objects-94572b17663
How to use the JavaScript Array sort() method to sort arrays of numbers, strings, and objects. | by Pan Liu | Medium
December 16, 2021 - If compare(a,b) returns zero, the sort() method considers a equals b and leaves their positions unchanged. In this case, initially the a value is 8 and the b value is 2, and the comparator function is going to do a three-way comparison, it will check whether the a value is less than, greater than or equal to the b value. const numbers = [8, 2, 100, 4];const sortedNumbers = numbers.sort((a, b) =>{ if (a < b) { return -1; } else if (a > b) { return 1; } else { return 0; } })console.log(sortedNumbers);
🌐
Mimo
mimo.org › glossary › javascript › sort
JavaScript Sort Method: Advanced Array Sorting Techniques
Sorting arrays in JavaScript is simple and versatile with the sort() method. This method allows you to sort arrays of numbers, strings, or objects with custom functions.
🌐
W3Schools
w3schools.com › js › js_array_sort.asp
JavaScript Array Sort
Using a sort function, like explained above, you can sort an numeric array in random order · const points = [40, 100, 1, 5, 25, 10]; points.sort(function(){return 0.5 - Math.random()}); ... The points.sort() method in the example above is not ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-sort-numeric-array-using-javascript
JavaScript - Sort a Numeric Array - GeeksforGeeks
The sort() method in JavaScript, when used with a comparison function, can sort the numeric arrays in ascending or descending order. This is the most frequently used approach. ... const a = [5, 2, 9, 1, 7]; // Ascending order a.sort((x, y) => ...
Published: August 5, 2025
🌐
Dmitri Pavlutin
dmitripavlutin.com › javascript-array-sort-numbers
array.sort() Does Not Simply Sort Numbers in JavaScript
January 26, 2021 - (a, b) => a - b is a short comparator to sort numbers. I recommend this form to sort the array of numbers in JavaScript.
🌐
Medium
medium.com › @jmin499 › javascript-sort-method-571d8bef61a1
JavaScript Sort Method. How to sort numbers inside an array… | by James Min | Medium
December 8, 2019 - JavaScript Sort Method How to sort numbers inside an array that’s unsorted? const numbers= [4, 1, 3, 2]; Easy! We can sort the numbers of array by using the sort() method. const numbers = [4, 1, 3 …
🌐
Mokkapps
mokkapps.de › tips › js › how-to-sort-an-array-of-integers
JavaScript Tip: How to Sort an Array of Integers | Michael Hoffmann
January 2, 2023 - To sort an array of integers, we must provide a compare function that defines the sort order: ... const numbers = [10, 3, 45, 999, 20] const ascendingSortedNumbers = [...numbers].sort((a, b) => a - b) console.log(ascendingSortedNumbers) // [ ...
🌐
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, ...
🌐
freeCodeCamp
freecodecamp.org › news › javascript-array-sort-tutorial-how-to-use-js-sort-methods-with-code-examples
JavaScript Array Sort – How to Use JS Sort Methods (With Code Examples)
May 24, 2020 - In this post, you will learn how to sort an array in JavaScript by using the sort( ) method for strings and numbers.
🌐
Envato Tuts+
code.tutsplus.com › home › javascript
Sorting Values With JavaScript | Envato Tuts+ - Code
February 24, 2023 - For this, we can make use of the following function: In the function, we use parseInt() to make sure the value is a number, and then we subtract both numbers to get a negative, positive, or zero value.