javascript

Sort array using Array.sort() in javascript

If you have an array of items in Javascript, you can use the Array.sort() function to sort them in alphabetical or numerical order. You can also specify a compare function to sort in a custom order.

Sort array using Array.sort() in javascript

In order to sort an array using the Javascript sort() function:

  1. Create an array.
  2. Use array.sort() function to sort array in ascending order.

Sort an array of alphabetic characters using sort() function

The array.sort() function is used to sort an array in alphabetical order. This function takes an array as a parameter and sorts the array in alphabetical order.

// create an array
const arr = ["Joe", "Victor", "Henna"];

// sort the array in ascending order
arr.sort();
console.log(arr);

// descending order
arr.reverse();
console.log(arr);

Output

["Henna","Joe","Victor"]
["Victor","Joe","Henna"]

Live Demo

This code example creates an array and sorts it in ascending order. It then sorts the array in descending order.


Sort an array of numeric values using sort() function

If you have an array of numeric values in JavaScript, you can use the sort() function to sort them in ascending or descending order. The sort() function takes an optional compare function as an argument that can be used to customize the sort order.

// create an array
var numeric_arr = [50, 20, 40, 60, 10, 30];

// sort in ascending order
numeric_arr.sort(function (a, b) { return a - b });
console.log(numeric_arr);

// sort in descending order
numeric_arr.sort(function (a, b) { return b - a });
console.log(numeric_arr);

Live Demo

If you are using ES6 then you can use the below code:

// create an array
const numeric_arr = [50, 20, 40, 60, 10, 30];

// sort in ascending order
numeric_arr.sort((a, b) => a - b );
console.log(numeric_arr);

// sort in descending order
numeric_arr.sort((a, b) => b - a);
console.log(numeric_arr);

Live Demo

Output

[10,20,30,40,50,60]
[60,50,40,30,20,10]

The first code block sorts the array in ascending order, while the second code block sorts the array in descending order.


Sort an array of objects using sort() function

Now, we will sort an array of objects using its key value in alphabetical order. We will use the callback function in the sort function that will compare the values.

// create an array of objects
var fruits = [
    { 'name': 'banana', 'color': 'yellow' },
    { 'name': 'apple', 'color': 'red' },
    { 'name': 'orange', 'color': 'orange' }
];

// sot array of object using name key value
fruits.sort((a, b) => a.name.localeCompare(b.name));

console.log(fruits);

Output

[
   {"name":"apple","color":"red"},
   {"name":"banana","color":"yellow"},
   {"name":"orange","color":"orange"}
]

Live Demo

This code creates an array of objects, each with a name and color key. The array is then sorted by the name key value using the .sort() method.

The .sort() method takes a callback function which takes two arguments, a and b. In this case, a and b are objects from the fruits array.

The callback function returns a value that determines whether a should come before b in the sorted array. In this case, the callback function is using the .localeCompare() method to compare the name key values of a and b.


Sort an array of objects by id value

Below is another example of sorting an array using the numeric value of the id key.

// create an array of objects
var students = [
    { id: 3, name: 'Sonia' },
    { id: 1, name: 'Aakash' },
    { id: 2, name: 'John' }
];

// sot array of objects using name key value
students.sort((a, b) => a.id - b.id);

console.log(students);

Output

[
   {"id":1,"name":"Aakash"},
   {"id":2,"name":"John"},
   {"id":3,"name":"Sonia"}
]

Live Demo

In the above code example:

We have created an array of objects with three student objects.

Sorted the array of objects by the id key value using a callback function.

Print the sorted array of objects to the console.


Conclusion

In this article, we have sorted an array in ascending and descending order. We have used the sort() function in order to do that. We have also explained different code examples of the sort() function with the callback function passed. If you have more code examples for the same then you can also contribute here.

Was this helpful?