js數(shù)組排序默認(rèn)使用字母順序,想要對整數(shù)數(shù)組排序:
function sortNumber(a,b) {
return a - b;
}
var numArray = [140000, 104, 99];
numArray.sort(sortNumber);
alert(numArray.join(","));
EDIT: using ES6 arrow functions:
numArray.sort((a, b) => a - b); // For ascending sort
numArray.sort((a, b) => b - a); // For descending sort
w3c教程中默認(rèn)的js排序和逆序舉例
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort(); // First sort the elements of fruits
fruits.reverse(); // Then reverse the order of the elements
按照數(shù)值排序
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b});