| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/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);
// : Array ["Dec", "Feb", "Jan", "March"]
const array = [1, 30, 4, 21, 100000];
array.sort();
console.log(array);
// : Array [1, 100000, 21, 30, 4]
sort()
sort(compareFn)
compareFn undefined UTF-16 "banana" "cherry" 9 80 Unicode "80" "9" undefined
:
UTF-16 \uFFFF Unicode 2 \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;
} else if ( a b ) {
return 1;
}
// a b
return 0;
}
compareFn(a, a) === 0 compareFn(a, b) compareFn(b, a) 0 compareFn(a, b) compareFn(b, c) 0 compareFn(a, c) 2 1, 0, -1 0 1 0 0 -1 0
a b Infinity NaN
function compareNumbers(a, b) {
return a - b;
}
4
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;
}
// names must be equal
return 0;
});
ASCII e, , , a, String.prototype.localeCompare() :
const items = ["rserv", "premier", "communiqu", "caf", "adieu", "clair"];
items.sort((a, b) => a.localeCompare(b));
// items ['adieu', 'caf', 'communiqu', 'clair', 'premier', 'rserv']
compareFn () compareFn compareFn map
//
const data = ["delta", "alpha", "charlie", "bravo"];
//
const mapped = data.map((v, i) => ({ 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] creates a shallow copy, so sort() does not mutate the original
const sorted = [...numbers].sort((a, b) => a - b);
sorted[0] = 10;
console.log(numbers[0]); // 3
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 }, // original maintained for similar grade (stable sorting)
{ name: "Devlin", grade: 15 }, // original maintained for similar grade (stable sorting)
];
: Alex Devlin
10 ECMAScript 2019
[
{ name: "Eagle", grade: 13 },
{ name: "Sam", grade: 14 },
{ name: "Devlin", grade: 15 }, // original order not maintained
{ name: "Alex", grade: 15 }, // original order not maintained
];
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 V8ChromeNode.js JavaScriptCoreSafari [3, 1, 4, 1, 5, 9] SpiderMonkeyFirefox [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]
5
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 |
Array.prototype.sort (core-js)ArrayArray.prototype.reverse()Array.prototype.toSorted()String.prototype.localeCompare()TypedArray.prototype.sort()Array.prototype.sort on v8.dev (2019)Array.prototype.sort stability by Mathias BynensArrayat()concat()copyWithin()entries()every()fill()filter()find()findIndex()findLast()findLastIndex()flat()flatMap()forEach()includes()indexOf()join()keys()lastIndexOf()map()pop()push()reduce()reduceRight()reverse()shift()slice()some()sort()splice()toLocaleString()toReversed()toSorted()toSpliced()toString()unshift()values()with()[Symbol.iterator]()Object/Function| Web Proxy Viewer | New URL | Original Page |