Array in JavaScript – sort() Method

sort() method for Array in JavaScript is used to sort the elements in that array.

Syntax is as below:

compareFunction: Specifies a function that defines the sort order. The array is sorted lexicographically if this function is skipped

The lexicographic or lexicographical order is a generalization of alphabetical order of words based on the alphabetical order of their component letters.

Return Value:
Returns a sorted array.

Example:

Output: This will give the following output:

You can Try sort() method yourself here.

You Might Interested In

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.