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];_x000D_
numArray.sort(function(a, b) {_x000D_
return a - b;_x000D_
});_x000D_
_x000D_
console.log(numArray);
_x000D_
In ES6, you can simplify this with arrow functions:
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. (Because Inf - Inf
is NaN, not 0).
Also examples of sorting objects by key.