In this tutorial, you will learn about the javascript array sort()
method with the help of multiple examples.
The sort()
method sorts the elements of an array in place in a specific order (ascending or descending).
Example
const months = ['March', 'Jan', 'Feb', 'Dec'];
//sort the array in ascending order
months.sort();
console.log(months);
// Output: [ 'Dec', 'Feb', 'Jan', 'March' ]
sort() Syntax
The Syntax of the sort()
method is:
arr.sort(compareFunction)
Here, arr
is an array.
sort() Parameter
sort()
method takes in :
compareFunction: it’s a function that defines the sorting order. If not specified, the array elements are converted to strings, and then sorted according to each character’s Unicode code point value.
sort() Return Value
sort()
method doesn’t return a new sorted array, instead it sorted the original array and returns its reference.
Example 1: Sorting the Array Elements
If we don’t pass a compareFunction
then,
const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// Output: Array ["Dec", "Feb", "Jan", "March"]
const array = [1, 30, 4, 21, 100000];
array.sort();
console.log(array);
// Output: Array [1, 100000, 21, 30, 4]
Output
[ 'Dec', 'Feb', 'Jan', 'March' ]
[ 1, 100000, 21, 30, 4 ]
In the above program, months are sorted in alphabetical order. For example, Dec
comes before March because “D”
comes before “M”
.
Along with this, we are also sorting an array in the above program. But if you look at the output, you will notice that the array is not actually sorted. So this happens if you don’t pass a compareFunction
in the sort()
method.
Here the array is not sorted numerically instead, the array elements are first converted to the string and then sorted alphabetically. For example, alphabetically “1”
comes before “2”
.
Example 2: Sorting using Compare Function
If we pass a compareFunction
in the sort()
method, then:
The array elements will be sorted according to the return value of the compareFunction
.
All undefined elements will be sorted at the end and the compare function will not apply to them.
const numbers = [2, 1, 5, 3, 4, 7, 6];
// function to compare
function compareFunction(a, b) {
return a - b;
}
// sorting numerically
numbers.sort(compareFunction);
console.log(numbers);
//Output: [ 1, 2, 3, 4, 5, 6, 7 ]
Output
[ 1, 2, 3, 4, 5, 6, 7 ]
Here, we have used a compareFunction
to sort the array elements numerically. The compareFunction
takes in two parameters a
and b
that represents two current values.
Let’s understand how compareFunction
works.
The compareFunction
returns a value and the array elements are sorted based on that return value.
compareFunction(a, b) return value | sort order |
---|---|
Returned value > 0 | sort a after b |
Returned value < 0 | sort a before b |
Returned value == 0 | keep original order of a and b |
Example 3: Sorting Array of Objects
const items = [
{ name: "John", age: 21 },
{ name: "David", age: 37 },
{ name: "Borus", age: 45 },
{ name: "Rishi", age: 12 },
];
// sort by age
items.sort((a, b) => a.age - b.age);
console.log(items);
// sort by name
items.sort((a, b) => {
const nameA = a.name.toUpperCase();
const nameB = b.name.toUpperCase();
if (nameA > nameB) {
return 1;
}
if (nameA < nameB) {
return -1;
}
return 0;
});
console.log(items);
Output
[ { name: 'Rishi', age: 12 },
{ name: 'John', age: 21 },
{ name: 'David', age: 37 },
{ name: 'Borus', age: 45 } ]
[ { name: 'Borus', age: 45 },
{ name: 'David', age: 37 },
{ name: 'John', age: 21 },
{ name: 'Rishi', age: 12 } ]
In the above program, we have sorted an array of objects by the name items.
We sorted the items in two ways, first according to age
and second according to name
.
we passed the arrow function inside sort()
method to sort the items
according to their name
and age
.
items.sort((a, b) => a.age - b.age);
Here, we sorted the items
according to their age.
items.sort((a, b) => {
const nameA = a.name.toUpperCase();
const nameB = b.name.toUpperCase();
if (nameA > nameB) {
return 1;
}
if (nameA < nameB) {
return -1;
}
return 0;
});
Here, we sorted the items
according to their name
.
Note: We are using toUpperCase()
to convert the names into uppercase.