How to sort an array of objects by a property value in JavaScript

Last Updated on :February 25, 2024

In JavaScript, you can sort arrays of objects based on one of their properties using the Array.prototype.sort() method. In this article, we will discuss how to sort an array of objects by a property value in Javascript.

Let’s start with an example. Consider the following array of objects.



var employee = [
 {
   "emp_name": "John",
   "emp_dept": "IT",
   "emp_age": 32,
   "emp_salary": 5400
 },
 {
   "emp_name": "Ram",
   "emp_dept": "Accounting",
   "emp_age": 36,
   "emp_salary": 4500
 },
 {
   "emp_name": "Billy",
   "emp_dept": "Manager",
   "emp_age": 42,
   "emp_salary": 7300
 },
 {
   "emp_name": "Raghu",
   "emp_dept": "Intern",
   "emp_age": 22,
   "emp_salary": 2250
 }
];

If you want to sort this array by the emp_age property in ascending order. To do this, you can call the sort() method on the employee array and provide a comparison function as an argument:


	employee.sort((a, b) => (a.emp_age > b.emp_age ) ? 1 : -1);

The “compare” function takes two arguments, often referred to as “a” and “b”.The function should return a positive number if “b” comes before “a” , negative number if “a” comes before “b”. zero if the two objects have the same value and their order doesn’t matter.

You can also sort the array in descending order by reversing the comparison:



 employee.sort((a, b) => (a.emp_age < b.emp_age ) ? 1 : -1);

You can sort an array of objects by any other property value by replacing emp_age with the other property that you want to sort by.

For example, to sort the employee array by the emp_name property in alphabetical order, you can use the following comparison function:



 employee.sort((a, b) => a.emp_name.localeCompare(b.emp_name));


The localeCompare() method compares two strings and returns a negative number if the first string comes before the second string, a positive number if the first string comes after the second string, or zero if the two strings are equal.

You can also create a dynamic sort function that sorts objects based on the property that you pass:



function dynamicSortFunc(property) {

  if (property === undefined) {
    return function (a, b) {
      return 0;
    }
  }

  return function (a, b) {
    var result;
    if (typeof a[property] == 'string') {
      result = a[property].localeCompare(b[property]);
    } else {
      result = (a[property] > b[property]) ? 1 : -1;
    }
    return result;
  }
}



You can sort the employee array by emp_name or emp_age property with dynamic sort function like below.




employee.sort(dynamicSortFunc("emp_name"));
employee.sort(dynamicSortFunc("emp_age"));

employee.sort(dynamicSortFunc());


If no parameter passed to dynamic sort function then no sorting will be performed.

Conclusion

In conclusion, sorting an array of objects by a property value in JavaScript is easy using the Array.prototype.sort() method and a comparison function. By providing a custom comparison function, you can sort the array by any property value and in any order.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *