[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/sort [Back]  [Original]

Array.prototype.sort() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Array.prototype.sort()

Baseline

20157

sort() Array (in-place) UTF-16

toSorted()

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]

js
sort()
sort(compareFn)

compareFn

a

undefined

b

undefined

  • a b
  • a b
  • 0 NaN a b

(a, b) => a - b

Unicode

compareFn undefined UTF-16 "banana" "cherry" 9 80 Unicode "80" "9" undefined

sort() 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

js
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

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

sort() this length

4

js
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']

js
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

ASCII e, , , a, String.prototype.localeCompare() :

js
const items = ["rserv", "premier", "communiqu", "caf", "adieu", "clair"];
items.sort((a, b) => a.localeCompare(b));

// items  ['adieu', 'caf', 'communiqu', 'clair', 'premier', 'rserv']

map

compareFn () compareFn compareFn map

js
// 
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]);

mapsort

sort()

sort()

js
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()

js
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

js
const students = [
  { name: "Alex", grade: 15 },
  { name: "Devlin", grade: 15 },
  { name: "Eagle", grade: 13 },
  { name: "Sam", grade: 14 },
];

grade

js
students.sort((firstItem, secondItem) => firstItem.grade - secondItem.grade);

students

js
[
  { 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

js
[
  { name: "Eagle", grade: 13 },
  { name: "Sam", grade: 14 },
  { name: "Devlin", grade: 15 }, // original order not maintained
  { name: "Alex", grade: 15 }, // original order not maintained
];

js
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

js
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

sort()

js
console.log(["a", "c", , "b"].sort()); // ['a', 'b', 'c', empty]
console.log([, undefined, "a", "b"].sort()); // ["a", "b", undefined, empty]

sort()

sort() this length 0 length - 1

js
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


Web Proxy Viewer  |  New URL  |  Original Page