JavaScript

Sort Array by Firstname (Alphabetically) in JavaScript

While coding in JavaScript, developers might need to sort the arrays in ascending order, descending order, or sometimes sort elements alphabetically. To do so, JavaScript provides a predefined method called the “sort()” method. When the developers need to sort an array alphabetically by any specific attribute such as “name”, “id”, and so on, use the “sort()” method with the comparator function.

This article will describe the procedure for sorting arrays by first names (alphabetically) in JavaScript.

How to Sort Array by Firstname (Alphabetically) in JavaScript?

To alphabetically sort an array by its first name, use the “sort()” method in conjunction with a comparator function. The comparator function should take two objects as arguments and return a negative(-ve) number if the first object should come before the second, 0(zero) if they are equal, and a positive(+ve) number if the first object should come after the second.

Example
Create an array of objects containing user information, such as names (firstName, lastName) and ages of the users:

let userInfo = [
{
 firstName: "Mari",
 lastName: "Liam",
 age: 25
},
{
 firstName: "Emma",
 lastName: "Noah",
 age: 20
},
{
 firstName: "Mia",
 lastName: "William",
 age: 14
},
{
 firstName: "Henry",
 lastName: "Lucas",
 age: 23
}
];

Now, sort the array by their firstNames. So, for this purpose, use the “sort()” method with the comparator function. The sort() method compares each value according to the specified criteria and each name is sorted according to the returned value when passing the comparison function to it:

userInfo.sort(function (x, y) {
 if (x.firstName  y.firstName) {
  return 1;
  }
  return 0;
});

In the following above code snippet:

  • First, call the “sort()” method with the array of an object and “comparator function” with two parameters “x” and “y”. Check if the “x.firstName” is less than “firstName” of variable “y”, return “-1” it means “x” comes before “y”.
  • If the “x.firstName” is greater than the “firstName” of variable “y”, return “1”. It signifies that the “x” comes after the “y”.
  • If the method returns zero, the order remains unchanged.

Print the sorted array on the console:

console.log(userInfo);

It can be observed that the array of user information has been successfully sorted alphabetically by their first names:

If you may have an array of words (names) and want to arrange each word alphabetically (from a-z), simply use the “sort()” method. The sorted array is returned by the sort() method, which arranges array elements as they are added to it.

Let’s say we have an array of names (not an object that contains first and last names):

let userNames = ["Mari", "Emma", "Mia", "Henry"];

Call the sort() method:

userNames.sort();

Print the sorted array of names on the console:

console.log(userNames);

Output

That’s all about the sorting array by first name (alphabetically) in JavaScript.

Conclusion

To alphabetically sort an array by its first name, use the “sort()” method in conjunction with a comparator function. If the function returns a negative value, the “x” is sorted before “y”, if it returns a positive, it means “y” is sorted before “x”. If it gives 0, values will place in the same position. This article described the procedure for sorting arrays by first names (alphabetically) in JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.