Sorting 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.
123const numbers = [1, 2, 10, 21]; numbers.sort(); console.log(numbers); // Output: 1, 10, 2, 21
To sort numbers correctly, use a compare function:
1234const numbers = [1, 2, 10, 21]; const ascending = [...numbers].sort((a, b) => a - b); console.log(ascending); // Output: 1, 2, 10, 21
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
});
ais considered as the first element;bis 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.
1234567const 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
Sort Strings
The localeCompare() method facilitates alphabetical sorting, allowing customization for both ascending and descending orders.
1234567const 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
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?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 2.27
Sorting 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.
123const numbers = [1, 2, 10, 21]; numbers.sort(); console.log(numbers); // Output: 1, 10, 2, 21
To sort numbers correctly, use a compare function:
1234const numbers = [1, 2, 10, 21]; const ascending = [...numbers].sort((a, b) => a - b); console.log(ascending); // Output: 1, 2, 10, 21
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
});
ais considered as the first element;bis 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.
1234567const 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
Sort Strings
The localeCompare() method facilitates alphabetical sorting, allowing customization for both ascending and descending orders.
1234567const 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
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?
Thanks for your feedback!