| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/sort | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..
sort() . (.). Unicode.
arr.sort([compareFunction])
compareFunction. , . , Unicode, .
compareFunction , Unicode. , "" "". , 9 80, , "80" "9" Unicode.
var fruit = ["", "", ""];
fruit.sort(); // ['', '', '']
var scores = [1, 2, 10, 21];
scores.sort(); // [1, 10, 2, 21]
var things = ["", "", "1 ", "2 "];
things.sort(); // ['1 ', '2 ', '', '']
// Unicode, ,
// , , .
compareFunction , . a b, :
compareFunction(a, b) 0, a , b, , a .compareFunction(a, b) 0, a b , . : ECMAscript , (, Mozilla , 2003 ).compareFunction(a, b) 0, b , a.compareFunction(a, b) a b. , ., :
function compare(a, b) {
if (a b ) {
return -1;
}
if (a b ) {
return 1;
}
// a b
return 0;
}
, , b a. :
function compareNumbers(a, b) {
return a - b;
}
var numbers = [4, 2, 5, 1, 3];
numbers.sort(function (a, b) {
return a - b;
});
console.log(numbers); // [1, 2, 3, 4, 5]
.
var items = [
{ name: "Edward", value: 21 },
{ name: "Sharpe", value: 37 },
{ name: "And", value: 45 },
{ name: "The", value: -12 },
{ name: "Magnetic" },
{ name: "Zeros", value: 37 },
];
items.sort(function (a, b) {
if (a.name > b.name) {
return 1;
}
if (a.name < b.name) {
return -1;
}
// a b
return 0;
});
, , . , .
var stringArray = ["", "", ""];
var numericStringArray = ["80", "9", "700"];
var numberArray = [40, 1, 5, 200];
var mixedNumericArray = ["80", "9", "700", 40, 1, 5, 200];
function compareNumbers(a, b) {
return a - b;
}
// ,
console.log("stringArray:", stringArray.join());
console.log(":", stringArray.sort());
console.log("numberArray:", numberArray.join());
console.log(" :", numberArray.sort());
console.log(
" compareNumbers:",
numberArray.sort(compareNumbers),
);
console.log("numericStringArray:", numericStringArray.join());
console.log(" :", numericStringArray.sort());
console.log(
" compareNumbers:",
numericStringArray.sort(compareNumbers),
);
console.log("mixedNumericArray:", mixedNumericArray.join());
console.log(" :", mixedNumericArray.sort());
console.log(
" compareNumbers:",
mixedNumericArray.sort(compareNumbers),
);
. , , , .
stringArray: ,, : ,, numberArray: 40,1,5,200 : 1,200,40,5 compareNumbers: 1,5,40,200 numericStringArray: 80,9,700 : 700,80,9 compareNumbers: 9,80,700 mixedNumericArray: 80,9,700,40,1,5,200 : 1,200,40,5,700,80,9 compareNumbers: 1,5,9,40,80,200,700
-ASCII , (e, , , a, ..), , , : 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']
(compareFunction) . , . , map . , , , , , , .
//
var list = ["", "", "", ""];
//
var mapped = list.map(function (el, i) {
return { index: i, value: el.toLowerCase() };
});
// ,
mapped.sort(function (a, b) {
if (a.value > b.value) {
return 1;
}
if (a.value < b.value) {
return -1;
}
return 0;
});
//
var result = mapped.map(function (el) {
return list[el.index];
});
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-array.prototype.sort |
This page was last modified on 24 . 2025 . by MDN contributors.
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()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()toSpliced()Array.prototype.toString()Array.prototype.unshift()Array.prototype.values()Array.prototype.with()Array.prototype[@@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()Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |