| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/sort | [Back] [Original] |
Get to know MDN better
const months = ["March", "Jan", "Feb", "Dec"];
months.sort();
console.log(months);
// Expected output: Array ["Dec", "Feb", "Jan", "March"]
const array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1);
// Expected output: Array [1, 100000, 21, 30, 4]
sort()
sort(compareFn)
compareFn undefined UTF-16 bananacherry9 80 Unicode 809 undefined
UTF-16 Unicode \uFFFF surrogate code unit \uD800 \uDFFF \uD855\uDE51 \uFF3A
compareFn undefined undefined compareFn
compareFn(a, b) |
|
|---|---|
| > 0 | a b [b, a] |
| < 0 | a b [a, b] |
| === 0 | a b |
function compareFn(a, b) {
if (a b) {
return -1;
}
if (a b) {
return 1;
}
// a b
return 0;
}
compareFn(a, a) === 0compareFn(a, b) compareFn(b, a) 0 compareFn(a, b) compareFn(b, c) compareFn(a, c) 10 -1 0 1 0 0 -1 0
a b Infinity NaN
function compareNumbers(a, b) {
return a - b;
}
const stringArray = ["Blue", "Humpback", "Beluga"];
const numberArray = [40, 1, 5, 200];
const numericStringArray = ["80", "9", "700"];
const mixedNumericArray = ["80", "9", "700", 40, 1, 5, 200];
function compareNumbers(a, b) {
return a - b;
}
stringArray.join(); // 'Blue,Humpback,Beluga'
stringArray.sort(); // ['Beluga', 'Blue', 'Humpback']
numberArray.join(); // '40,1,5,200'
numberArray.sort(); // [1, 200, 40, 5]
numberArray.sort(compareNumbers); // [1, 5, 40, 200]
numericStringArray.join(); // '80,9,700'
numericStringArray.sort(); // ['700', '80', '9']
numericStringArray.sort(compareNumbers); // ['9', '80', '700']
mixedNumericArray.join(); // '80,9,700,40,1,5,200'
mixedNumericArray.sort(); // [1, 200, 40, 5, '700', '80', '9']
mixedNumericArray.sort(compareNumbers); // [1, 5, '9', 40, '80', 200, '700']
const items = [
{ name: "Edward", value: 21 },
{ name: "Sharpe", value: 37 },
{ name: "And", value: 45 },
{ name: "The", value: -12 },
{ name: "Magnetic", value: 13 },
{ name: "Zeros", value: 37 },
];
// value
items.sort((a, b) => a.value - b.value);
// name
items.sort((a, b) => {
const nameA = a.name.toUpperCase(); //
const nameB = b.name.toUpperCase(); //
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// name
return 0;
});
ASCII ea String.localeCompare
var items = ["rserv", "premier", "clich", "communiqu", "caf", "adieu"];
items.sort(function (a, b) {
return a.localeCompare(b);
});
// items ['adieu', 'caf', 'clich', 'communiqu', 'premier', 'rserv']
compareFn compareFn compareFn map()
//
const data = ["delta", "alpha", "charlie", "bravo"];
//
const mapped = data.map((v, i) => {
return { i, value: someSlowOperation(v) };
});
//
mapped.sort((a, b) => {
if (a.value > b.value) {
return 1;
}
if (a.value < b.value) {
return -1;
}
return 0;
});
const result = mapped.map((v) => data[v.i]);
sort()
const numbers = [3, 1, 4, 1, 5];
const sorted = numbers.sort((a, b) => a - b);
// numbers sorted [1, 1, 3, 4, 5]
sorted[0] = 10;
console.log(numbers[0]); // 10
sort() map() toSorted() sort() Array.from()
const numbers = [3, 1, 4, 1, 5];
// [...numbers] sort()
const sorted = [...numbers].sort((a, b) => a - b);
sorted[0] = 10;
console.log(numbers[0]); // 3
EcmaScript 10 EcmaScript 2019 Array.prototype.sort
const students = [
{ name: "Alex", grade: 15 },
{ name: "Devlin", grade: 15 },
{ name: "Eagle", grade: 13 },
{ name: "Sam", grade: 14 },
];
grade
students.sort((firstItem, secondItem) => firstItem.grade - secondItem.grade);
students
[
{ name: "Eagle", grade: 13 },
{ name: "Sam", grade: 14 },
{ name: "Alex", grade: 15 }, // grade
{ name: "Devlin", grade: 15 }, // grade
];
Alex Devlin
EcmaScript 10 EcmaScript 2019
[
{ name: "Eagle", grade: 13 },
{ name: "Sam", grade: 14 },
{ name: "Devlin", grade: 15 }, //
{ name: "Alex", grade: 15 }, //
];
const arr = [3, 1, 4, 1, 5, 9];
const compareFn = (a, b) => (a > b ? 1 : 0);
arr.sort(compareFn);
compareFn a > b 1 a b 0 V8 ChromeNode.js JavaScriptCore Safari [3, 1, 4, 1, 5, 9] SpiderMonkey Firefox [1, 1, 3, 4, 5, 9]
compareFn -1 0
const arr = [3, 1, 4, 1, 5, 9];
const compareFn = (a, b) => (a > b ? -1 : 0);
arr.sort(compareFn);
V8 JavaScriptCore [9, 5, 4, 3, 1, 1] SpiderMonkey [3, 1, 4, 1, 5, 9]
console.log(["a", "c", , "b"].sort()); // ['a', 'b', 'c', empty]
console.log([, undefined, "a", "b"].sort()); // ["a", "b", undefined, empty]
sort() this length 0 length - 1
const arrayLike = {
length: 3,
unrelated: "foo",
0: 5,
2: 4,
};
console.log(Array.prototype.sort.call(arrayLike));
// { '0': 4, '1': 5, length: 3, unrelated: 'foo' }
| ECMAScript 2027 LanguageSpecification # sec-array.prototype.sort |
ArrayArray.prototype.at()Array.prototype.concat()Array.prototype.copyWithin()Array.prototype.entries()Array.prototype.every()Array.prototype.fill()Array.prototype.filter()Array.prototype.find()Array.prototype.findIndex()findLast()Array.prototype.findLastIndex()Array.prototype.flat()Array.prototype.flatMap()Array.prototype.forEach()Array.prototype.includes()Array.prototype.indexOf()Array.prototype.join()Array.prototype.keys()Array.prototype.lastIndexOf()Array.prototype.map()Array.prototype.pop()Array.prototype.push()Array.prototype.reduce()Array.prototype.reduceRight()Array.prototype.reverse()Array.prototype.shift()Array.prototype.slice()Array.prototype.some()Array.prototype.sort()Array.prototype.splice()Array.prototype.toLocaleString()Array.prototype.toReversed()Array.prototype.toSorted()Array.prototype.toSpliced()Array.prototype.toString()Array.prototype.unshift()Array.prototype.values()Array.prototype.with()Array.prototype[Symbol.iterator]()Object/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()| Web Proxy Viewer | New URL | Original Page |