sort() method which allows us to sort an array of numbers, string and objects.
In this tutorial, you will understand how to
sort() method works and how to use it to sort array and object on the basis of various different parameters.
- Sort array of numbers
- Sort array of string
- Sort array of objects
sort() method sorts an array alphabetically and returns the array. The default sort order is ascending.
Example of sorting a string array.
sort() method converts elements into string and then compare there sequence of UTF-16 code units values to sort them.
Because of the above fact if you apply the
sort method directly on an array of numbers it does not get sorted by the number value.
In the output of above example the
sort() method places 20 before 3 and 61 before 8 because the numbers get converted to string and "20" comes before "3" when compare their UTF-16 code units values.
Then what should you do to sort the array elements in increasing order by number value?
To fix this you need to pass a compare function in sort method.
sort() method accepts a function as an optional argument. This function determines the order of sorting the elements.
The following is the syntax of
sort() method with compare function.
Note: If compare function is not supplied then all valid array elements are sorted by converting them to strings and comparing strings in UTF-16 code units.
The compare function in the
sort method accepts 2 arguments generally called a and b.
a and b are 2 numbers of the array that are being compared at any moment of the function execution. The compare function sorts the element on the basis of the return value of compare function.
If these numbers are being compared then:
- If compareFunction(a, b) returns less than 0, then
sort()method sorts a at lower index than b. Simply a will come before b.
- If compareFunction(a, b) returns equal to 0, then
sort()method leave the element positions as it is.
- If compareFunction(a, b) returns greater than 0, then
sort()method sorts a at greater index than b. Simply a will come after b.
Let's use the concept of compare function and sort an array of numbers.
If array elements are numbers and are not
Infinity then instead of returning on the base of 3 conditions as in above example simply return
a - b which will be equivalent to above 3 conditions.
Let's check for first 3 elements of the array. i.e
[61, 8, 20]
compareFunction(61 ,8); // return 53, a > b compareFunction(61 ,20); // return 41, a > b compareFunction(8 ,20); // return -12, a < b
You can also define function expression within
Or you can define compare function using an arrow function.
Or simply you can write. (arrow function as function expression)
Sort array of string
sort() method on the array of string and by default, it will sort an array of a string in ascending order.
But how will you sort the array in descending order?
To compare string in reverse order use compare function. You need to change the logic of compare function and pass it to
sort method as shown in the code below.
Or simply use the
reverse method on the array.
Sorting non-ASCII characters
To sort string with non-ASCII characters, i.e. String from other language é, è, a, ä, etc. Use
This function can compare non-ASCII characters so they appear in the right order.
To sort an array of object you can not directly use
sort() method. You will have to use compare function to decide the order of object depending on different property values.
Use compare function and return on the basis of the property. example
a.age - b.age see in the code below.
Output using console.table
Let's sort the object on the basis of joining date property.
Since 'joining' in the object is string value, first convert the value of joining string value to a date object and then use this in compare function to return value.
Output using console.table
Sorting objects by string property
To sort the employees array by their name convert employee name to lowercase and pass it in compare function.
Conclusion: In this tutorial, we learn how to sort an array of numbers, strings and objects using built-in