Definition and Usage
The sort() function sorts array elements.
Features
- By default, each element is converted to a string and sorted according to **Unicode code point** order.
- Specifying a sort order with a **callback function** as needed
- This function modifies the original array directly and does not return a new array.
- Unicode code point
- A Unicode code point is a unique numeric value assigned to identify each character.
These Unicode code point values are standardized and ordered across a wide range of writing systems and languages, including letters, characters from various languages, and emoji. Characters can be sorted based on the magnitude of these numeric values.
Basic Example
/* Default sorting: An example of sorting a string array
in Unicode code point order */
const enStrings = ["d", "b", "c", "a"];
enStrings.sort();
console.log(enStrings); // ["a", "b", "c", "d"]
/* An example of sorting numbers in ascending order
based on their numeric values using a callback function */
const sortedNumbers = [1, 4, 10, 31, 1000];
sortedNumbers.sort((a, b) => a - b);
console.log(sortedNumbers); // [1, 4, 10, 31, 1000]
Syntax
arr.sort();
arr.sort(compareFunction);
arr is the array to which the sort() function is applied.
Parameters
compareFunction |
Optional. A callback function that defines the sorting order.
|
|---|
Return value
Returns the sorted array. The return value is the modified original array itself; it does not return a new array.
Usage Notes
The default sorting behavior of the sort() function converts each element to a string and follows **Unicode code point** order. If necessary, you can specify a sorting order by passing a callback function.
This section explains what the default sorting order based on **Unicode code point** values means, and how to specify a sorting order using a **callback function** when needed.
**Unicode code point** sort order
When using the default sorting behavior, the sort() function converts each element to a string and sorts array elements according to Unicode code point order.
A Unicode code point value is a unique numeric value assigned to identify each character.
These Unicode code point values are standardized and ordered across a wide range of languages, including English and emoji. By comparing the magnitude of these numeric values, you can determine which characters come before or after others. This makes it possible to sort array elements using this criterion.
For example, consider the following table.
| Category | Character | Unicode code point value (decimal) |
|---|---|---|
| Uppercase letter | A |
65 |
| Uppercase letter | B |
66 |
| Uppercase letter | C |
67 |
| Lowercase letter | a |
97 |
| Lowercase letter | b |
98 |
| Lowercase letter | c |
99 |
| Digit | 0 |
48 |
| Digit | 1 |
49 |
| Digit | 2 |
50 |
| Emoji | 😃 |
128512 |
| Emoji | 🙊 |
128586 |
As shown in the table above, understanding the order of Unicode code point values allows you to perform lexicographical sorting based on Unicode code points and to compare the relative order of specific characters.
Using the table above as a reference, check how the following example produces its result.
/* Example: Sorting an array of alphabetic characters */
const enStrings = ["d", "b", "c", "a"];
enStrings.sort();
console.log(enStrings); // ["a", "b", "c", "d"]
/* Example: Sorting an array of numeric characters */
const numStrings = [0, 1, 2];
numStrings.sort();
console.log(numStrings); // [0, 1, 2]
For the numeric values assigned to Unicode code points, refer to Wikipedia's List of Unicode characters.
Specifying a sort order with a **callback function** as needed
By default, the sort() function converts each element to a string and follows Unicode code point order. However, when necessary, you can specify a custom sort order by passing a callback function as an argument.
arr.sort();
arr.sort(compareFn); // compareFunction (optional): pass a callback function to define the sort order
Description of the callback function (compareFunction)
The structure of the callback function (compareFunction) is as follows.
/**
* Callback function
*
* @param a The first element to compare. This value is never undefined.
* @param b The second element used for comparison. This value is never undefined.
*
* The callback function can be defined as a named function (user-defined function)
* or as an anonymous function.
* (Naturally,) any callback function can also be written as an arrow function.
*/
function compareFunction(a, b) {
// Logic for comparing a and b: must return a comparison result
}
When a callback function (compareFunction) is provided as an argument to the sort() function, it receives two elements at a time, compares them, and determines their order based on the returned value.
- If the return value is less than
0, the first argument is placed before the second argument. - If the return value is
0, the order remains unchanged. - If the return value is greater than
0, the first argument is placed after the second argument.
Using this mechanism, you can perform custom sorting. For example, the following function sorts numbers in ascending order.
const numbers = [1, 1000, 10, 31];
function compareNumbers(a, b) {
return a - b;
}
numbers.sort(compareNumbers);
console.log(numbers); // Output: [1, 10, 31, 1000]
In the example above, the compareNumbers() function receives two numbers as arguments and returns a negative value when the second number is greater than the first, a positive value when the first number is greater than the second, and 0 when both numbers are equal.
As a result, the numbers array is sorted in ascending order using the compareNumbers() function.
Things to Keep in Mind
When using the sort() function to sort an array, there are several important points to keep in mind.
Sparse Arrays and Elements with undefined Values
The sort() function sorts array elements based on their values.
However, when the array contains elements with no assigned value (sparse arrays) or elements whose value is undefined, those elements are always placed at the end of the array.
const sparseArr = [3, , 1, undefined, 2];
sparseArr.sort();
console.log(sparseArr);
// Example output: [1, 2, 3, undefined, <1 empty slot>]
// Elements with the value undefined or missing elements (sparse array slots)
// are placed at the end of the array after sorting.
For this reason, in real-world development, it is recommended to clean up sparse arrays first—such as by using the filter() function—before sorting them, in order to achieve predictable results.
Additional Explanation
The filter() function for arrays filters array elements using a callback function. When you define a condition in the callback function, only the elements that satisfy the condition
are returned in a new array.
const arr = [3, , 1, undefined, 2];
function hasValue(value) {
// Remove elements whose value is undefined.
return value !== undefined;
}
/*
* filter() behaves as follows:
*
* 1. Empty slots in a sparse array do not invoke the callback function,
* so they are automatically excluded.
* 2. Elements with the value undefined are excluded because the callback
* function returns false for them.
*/
const filteredArr = arr.filter(hasValue);
filteredArr.sort();
console.log(filteredArr); // [1, 2, 3]
Misconceptions About **Unicode Code Point** Sort Order
The default sorting behavior of the sort() function converts each element to a string
and sorts the array based on the **Unicode code point** order assigned to each character.
With some strings, this sorting behavior may appear to produce a natural or expected order.
const enStrings = ["A", "C", "B"];
enStrings.sort();
console.log(enStrings); // ["A", "B", "C"]
The result of the example above can be easily predicted by looking at the following **Unicode code point** values.
| Category | Character | Unicode code point value |
|---|---|---|
| Uppercase letter | A |
65 |
| Uppercase letter | B |
66 |
| Uppercase letter | C |
67 |
However, the following example clearly shows the limitations of the default sorting behavior.
const enStrings = ["A", "a", "B"];
enStrings.sort();
console.log(enStrings); // ["A", "B", "a"] → the lowercase "a" is sorted after the uppercase "B"
This sorting result does not match the natural order that humans typically expect. Instead of ["A", "B", "a"], we would usually expect ["A", "a", "B"].
What you should be aware of is that **Unicode code point** sort order can produce results that differ from the **natural order** humans intuitively expect. As shown below, the Unicode code point value of the lowercase letter a is 97, which is greater than the Unicode code point value of the uppercase letter B, which is 66. For this reason, when ["A", "a", "B"] is sorted using the default sort() behavior, the result becomes ["A", "B", "a"].
| Category | Character | Unicode code point value |
|---|---|---|
| Uppercase letter | A |
65 |
| Uppercase letter | B |
66 |
| Lowercase letter | a |
97 |
Sorting Numeric Arrays
Extra caution is required when sorting numeric arrays.
By default, the sort() function treats each array element as a string, even if the elements are numbers. However, this does not mean that the elements themselves are converted into strings. Instead, the string-converted values are used only for comparison during the sorting process. As a result, the sorted array contains the original element values, not strings.
Let's look at an example.
/* Example of sorting a numeric array:
The numeric elements are compared as strings and sorted
according to Unicode code point order */
const numbers = [1, 1000, 4, 10, 31];
numbers.sort();
console.log(numbers); // Output: [1, 10, 1000, 31, 4] <= Warning! Sorted as strings
This result may be unexpected if you are not aware that the sort() function compares numeric elements
as strings by default.
To sort a numeric array correctly, you must provide a comparison function as the callback function parameter.
const numbers = [1, 1000, 4, 10, 31];
function compareNumbers(a, b) {
return a - b;
}
numbers.sort(compareNumbers);
console.log(numbers); // Output: [1, 4, 10, 31, 1000]
This ensures that the numeric array is sorted correctly in ascending order.
Summary of Sorting Methods by Scenario
The sort() function is commonly used to sort arrays, but depending on the situation, it can produce results that differ from what you might expect. The following table summarizes the **sorting methods by scenario** for the sort() function
that we have covered so far.
| Sorting target | Sorting method (code example) | Notes |
|---|---|---|
| Simple strings | arr.sort() |
Sorted based on Unicode code point values (uppercase > lowercase) |
| Numbers (ascending) |
arr.sort(function (a, b) {
return a - b;
});
|
Sorted by numeric value in the natural order humans expect |
| Numbers (descending) |
arr.sort(function (a, b) {
return b - a;
});
|
Sorted from larger numbers to smaller ones |
| After data cleanup |
arr.filter(function (value) {
return value !== undefined;
}).sort(...)
|
Sort after removing sparse elements or undefined values |
Compatibility
| Method |
Desktop Chrome
|
DesktopDesktop Edge
|
Desktop Firefox
|
Safari
|
Node.js
|
|---|---|---|---|---|---|
sort()
|
1 | 12 | 1 | 1 | 0.10 |
Specifications
| Specification | |
|---|---|
sort()
|
ECMAScript® 2026 Language Specification #sec-array.prototype.sort |