JavaScript array sort method | Sorts items order of an Array

JavaScript array sort method is used to sorting Array items in the given order. A sorted Array can be any order (as per given/need) like, alphabetic or numeric, and either ascending or descending.

Note: Be default a sort() method sorts the values as strings in alphabetical and ascending order and changes the original array.



A function that defines an alternative sort order but its and Optional.

Return Value:

It returns Array object, with the items sorted.

Examples of sorting an Array in JavaScript

Let’s see the multiple examples:-

Sort string Array

Example of JavaScript sort array of objects alphabetically.


JavaScript sort array of objects alphabetically

Sort numbers in an array 

Using compareFunction.

Ascending order

Output: 1,1,2,4,5,10

Descending order

Output: 10,5,4,2,1,1

Q: How to JavaScript sort an array of objects by date?

Answer: follow this approach to sort dates in JS.


Do comment if you have any doubts and suggestion on this tutorial.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.