Sort Alphabetically JavaScript | Example code

Given array need to sort by firstname using JavaScript.

Sort Alphabetically JavaScript Example code:

Use user.sort and pass a function that takes two arguments and compares them (comparator).

How’s work:-

  • Something negative if the first argument is less than second (should be placed before the second in resulting array)
  • something positive if the first argument is greater (should be placed after the second one)
  • 0 if those two elements are equal.



Sort Alphabetically JavaScript example

Shortest possible code with ES6!

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

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version

Leave a Reply

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