.sort()
The .sort()
method returns an array with its items sorted in place.
Syntax
// No parameters
array.sort();
// With optional function
array.sort((firstElement, secondElement) => { /* function body */ };
If the .sort()
method is used with no arguments, all items with undefined
values are shifted to the end of the array while the remaining items are converted to strings and sorted by Unicode code point value.
An optional function is used to define how items are sorted. This is done by iterating over the array
and comparing every firstElement
and secondElement
in the /* function body */
.
Example
In the following example, the .sort()
method is applied to two arrays, letters
and numbers
(a mix of floats and integers):
const letters = ['d', 'b', 'e', 'a', 'c'];const numbers = [5, 2, 123, 5.01, 43.5];console.log('Letters: ', letters.sort());console.log('Numbers: ', numbers.sort());
This results in the following output:
Letters: [ 'a', 'b', 'c', 'd', 'e' ]Numbers: [ 123, 2, 43.5, 45, 5, 5.01 ]
The letters
were sorted in alphabetical order. The items in numbers
were sorted based on the leading number in the item’s value (e.g., their Unicode value). Sorting numerical values more strictly requires a custom comparison function.
Codebyte Example
The following example showcases how the optional callback
argument can be used to sort a numbers
array in ascending and descending order:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours