# javascript js数组自定义排序 - 如何正确排序整数数组

## js数组排序 js对数组对象排序 (12)

array.sort在默认情况下进行字典式排序，对于数字排序，提供您自己的函数。 这里有一个简单的例子：

``````function compareNumbers(a, b)
{
return a - b;
}

numArray.sort(compareNumbers);
``````

``````var numArray = [140000, 104, 99];
numArray = numArray.sort();
alert(numArray[0] + ", " + numArray[numArray.length - 1]);
``````

Array.prototype.sort（）是排序数组的方法，但有几个问题需要注意。

``````numArray.sort(function(a, b)
{
return a - b;
});
``````

``````numArray.sort(function(a, b)
{
return b - a;
});
``````

``````numArray = numArray.sort((a, b) => a - b);
``````

``````var a = [5, 17, 29, 48, 64, 21];
function sortA(arr) {
return arr.sort(function(a, b) {
return a - b;
})
;}
``````

``````...
function sortNumber(a, b)
{
return a - b;
}

numArray = numArray.sort(sortNumber);
``````

``````var numArray = [140000, 104, 99];
for (var i = 0; i < numArray.length; i++) {
var target = numArray[i];
for (var j = i - 1; j >= 0 && (numArray[j] > target); j--) {
numArray[j+1] = numArray[j];
}
numArray[j+1] = target
}
console.log(numArray);``````

``````var numArray = [140000, 104, 99];
for (var i = 0; i < numArray.length; i++) {
var target = numArray[i];
for (var j = i - 1; j >= 0 && (numArray[j] < target); j--) {
numArray[j+1] = numArray[j];
}
numArray[j+1] = target
}
console.log(numArray);``````

``````var numArray = [140000, 104, 99];
for (var i = 0; i < numArray.length - 1; i++) {
var min = i;
for (var j = i + 1; j < numArray.length; j++) {
if (numArray[j] < numArray[min]) {
min = j;
}
}
if (min != i) {
var target = numArray[i];
numArray[i] = numArray[min];
numArray[min] = target;
}
}
console.log(numArray);``````

``````var numArray = [140000, 104, 99];
for (var i = 0; i < numArray.length - 1; i++) {
var min = i;
for (var j = i + 1; j < numArray.length; j++) {
if (numArray[j] > numArray[min]) {
min = j;
}
}
if (min != i) {
var target = numArray[i];
numArray[i] = numArray[min];
numArray[min] = target;
}
}
console.log(numArray);``````

``````function sortArrayOfElements(arrayToSort) {
function compareElements(a, b) {
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
}

return arrayToSort.sort(compareElements);
}

e.g. 1:
var array1 = [1,2,545,676,64,2,24]
**output : [1, 2, 2, 24, 64, 545, 676]**

var array2 = ["v","a",545,676,64,2,"24"]
**output: ["a", "v", 2, "24", 64, 545, 676]**
``````

``````function sortArrayOfObjects(arrayToSort, key) {
function compareObjects(a, b) {
if (a[key] < b[key])
return -1;
if (a[key] > b[key])
return 1;
return 0;
}

return arrayToSort.sort(compareObjects);
}

e.g. 1: var array1= [{"name": "User4", "value": 4},{"name": "User3", "value": 3},{"name": "User2", "value": 2}]

**output : [{"name": "User2", "value": 2},{"name": "User3", "value": 3},{"name": "User4", "value": 4}]**
``````

``````return a - b;
``````

``````return a > b ? 1 : a < b ? -1 : 0;
``````

``````function sortNumber(a,b) {
return a - b;
}

var numArray = [140000, 104, 99];
numArray.sort(sortNumber);
``````

``````numArray.sort((a, b) => a - b);
``````

HTML：

``````<div id="demo"></div>
``````

JavaScript代码：

``````<script>
(function(){
var points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points;
points.sort(function(a, b){return a-b});
document.getElementById("demo").innerHTML = points;
})();
</script>
``````

``````var numArray = [140000, 104, 99];

// ES5
numArray = numArray.sort(function (a, b) {  return a - b;  });

// ES2015
numArray = numArray.sort((a, b) => a - b);

//outputs: 99, 104, 140000
``````

``````var arr = [
"apple",
"banana",
"cherry"
];

var arr2 = [
"dragonfruit",
"elderberry",
"fig"
];

arr.push(...arr2);
``````