Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Sorting Arrays with the sort() Method | Advanced Array Methods and Transformations
JavaScript Data Structures

bookSorting Arrays with the sort() Method

sort()

The sort() method rearranges elements of an array in place and returns the updated array. By default, sorting works well for strings but can behave unexpectedly with numbers.

Default Sorting Behavior

The sort() method converts elements to strings by default and compares them in Unicode order. That's why it works naturally for strings, but numeric arrays can produce unexpected results unless you provide a compare function.

123
const numbers = [1, 2, 10, 21]; numbers.sort(); console.log(numbers); // Output: 1, 10, 2, 21
copy

To sort numbers correctly, use a compare function:

1234
const numbers = [1, 2, 10, 21]; const ascending = [...numbers].sort((a, b) => a - b); console.log(ascending); // Output: 1, 2, 10, 21
copy

Custom Sort Orders

To tailor sorting to specific needs, a callback function is employed. This function, often called a compare function, dictates the sorting logic. Let's explore custom sorting for both numbers and strings. Basic syntax:

array.sort((a, b) => {
  // Callback body
});
  • a is considered as the first element;
  • b is considered as the second element.

Sort Numbers

In this instance, the compare function dictates ascending and descending orders based on the relationship between elements a and b.

1234567
const numbers = [34, 25, 19, 1, 92, 2, 3]; const ascendingNumbers = [...numbers].sort((a, b) => a - b); console.log(ascendingNumbers); // Output: 1, 2, 3, 19, 25, 34, 92 const descendingNumbers = [...numbers].sort((a, b) => b - a); console.log(descendingNumbers); // Output: 92, 34, 25, 19, 3, 2, 1
copy

Sort Strings

The localeCompare() method facilitates alphabetical sorting, allowing customization for both ascending and descending orders.

1234567
const employees = ["Antonia", "Rene", "Casey", "Lorraine", "Shelia"]; const inAlphabetOrder = [...employees].sort((a, b) => a.localeCompare(b)); console.log(inAlphabetOrder); // Antonia, Casey, Lorraine, Rene, Shelia const inReversedOrder = [...employees].sort((a, b) => b.localeCompare(a)); console.log(inReversedOrder); // Shelia, Rene, Lorraine, Casey, Antonia
copy

1. What is a key characteristic of the sort() method?

2. In the default sorting behavior of the sort() method, how does it treat elements?

3. In the example below, what will be the output?

question mark

What is a key characteristic of the sort() method?

Select the correct answer

question mark

In the default sorting behavior of the sort() method, how does it treat elements?

Select the correct answer

question mark

In the example below, what will be the output?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 7

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookSorting Arrays with the sort() Method

Swipe to show menu

sort()

The sort() method rearranges elements of an array in place and returns the updated array. By default, sorting works well for strings but can behave unexpectedly with numbers.

Default Sorting Behavior

The sort() method converts elements to strings by default and compares them in Unicode order. That's why it works naturally for strings, but numeric arrays can produce unexpected results unless you provide a compare function.

123
const numbers = [1, 2, 10, 21]; numbers.sort(); console.log(numbers); // Output: 1, 10, 2, 21
copy

To sort numbers correctly, use a compare function:

1234
const numbers = [1, 2, 10, 21]; const ascending = [...numbers].sort((a, b) => a - b); console.log(ascending); // Output: 1, 2, 10, 21
copy

Custom Sort Orders

To tailor sorting to specific needs, a callback function is employed. This function, often called a compare function, dictates the sorting logic. Let's explore custom sorting for both numbers and strings. Basic syntax:

array.sort((a, b) => {
  // Callback body
});
  • a is considered as the first element;
  • b is considered as the second element.

Sort Numbers

In this instance, the compare function dictates ascending and descending orders based on the relationship between elements a and b.

1234567
const numbers = [34, 25, 19, 1, 92, 2, 3]; const ascendingNumbers = [...numbers].sort((a, b) => a - b); console.log(ascendingNumbers); // Output: 1, 2, 3, 19, 25, 34, 92 const descendingNumbers = [...numbers].sort((a, b) => b - a); console.log(descendingNumbers); // Output: 92, 34, 25, 19, 3, 2, 1
copy

Sort Strings

The localeCompare() method facilitates alphabetical sorting, allowing customization for both ascending and descending orders.

1234567
const employees = ["Antonia", "Rene", "Casey", "Lorraine", "Shelia"]; const inAlphabetOrder = [...employees].sort((a, b) => a.localeCompare(b)); console.log(inAlphabetOrder); // Antonia, Casey, Lorraine, Rene, Shelia const inReversedOrder = [...employees].sort((a, b) => b.localeCompare(a)); console.log(inReversedOrder); // Shelia, Rene, Lorraine, Casey, Antonia
copy

1. What is a key characteristic of the sort() method?

2. In the default sorting behavior of the sort() method, how does it treat elements?

3. In the example below, what will be the output?

question mark

What is a key characteristic of the sort() method?

Select the correct answer

question mark

In the default sorting behavior of the sort() method, how does it treat elements?

Select the correct answer

question mark

In the example below, what will be the output?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 7
some-alt