Sort Array of objects by two properties in JavaScript


Given problem says to sort the given array of objects by two properties and create a program in javascript.

Understanding the problem

Let's have a deep understanding of the problem statement. You will be given an array of objects with key and value. As per the problem statement we need a sorted form of that array. To implement this problem we will first sort the first column of array and then second column of array. And then again arrange it in sorted form.

As we understand arrays are basic data structures in computer science. Array can also be a collection of objects and these objects can be in key value pairs. In which key will define the type of value in the array. So with the help of a key we can access it and perform sorting operations.

In our project we will solve the problem with two methods.

Algorithm - First Method

Step 1: Before understanding the logic first define an array with objects. The array is the initial point of the program so first understand the array we are going to use. In our first program the name of the array will be details and here name and age is defined as key and its values.

Step 2: This step will perform a sort operation on the defined array. In this step, the sorted data will be passed to the afterSorting variable. In the last step we will afterSorting to print the result.

Step 3: This step will go through multiple if statements. In the first line it will check the first two values of the name and if the first value is less than second then return -1 and in the second line it will check exactly opposite of the first line.

Step 4: This step will also perform multiple if statements and check the values for age. If the first value of age is less than the second value than return -1 otherwise return 1.

Step 5: This step will return the value of the updated and sorted array of objects.

Example

//define array
const details = [
  { name: 'Preeti', age: 25 },
  { name: 'Aman', age: 30 },
  { name: 'Shekhar', age: 20 },
  { name: 'Preeti', age: 25 },
  { name: 'Aman', age: 25 },
  { name: 'Anjali', age: 22 },
  { name: 'Kajal', age: 28 },
  { name: 'Anjali', age: 22 },
  { name: 'Shekhar', age: 20 },
];

const afterSorting = details.sort((first, second) => {
  if (first.name < second.name) return -1;
  if (first.name > second.name) return 1;
   if (first.age < second.age) return -1;
  if (first.age > second.age) return 1;
  return 0;
});

console.log(afterSorting);

Output

[
  { name: 'Aman', age: 25 },
  { name: 'Aman', age: 30 },
  { name: 'Anjali', age: 22 },
  { name: 'Anjali', age: 22 },
  { name: 'Kajal', age: 28 },
  { name: 'Preeti', age: 25 },
  { name: 'Preeti', age: 25 },
  { name: 'Shekhar', age: 20 },
  { name: 'Shekhar', age: 20 }
]

Algorithm - Second Method

This method is slightly different from the above one in terms of coding and syntax.

Step 1: In the initial step we need to define an array. In our case we have array named languages in which we have two properties defined: name and difficulty. So this data is self explanatory.

Step 2: This is the first step of our logic in this program. We have defined a variable in const called sortedArray. This function is taking data to sort and store the resultant array in it. Then we have created if else statements to check the conditions as per requirements of our program.

Example

// define array named languages with name and difficulty 
const languages = [
   {name: "Python", difficulty: "Easy"},
   {name: "Java", difficulty: "Tough"},
   {name: "Javascript", difficulty: "Medium"},
   {name: "Javascript", difficulty: "Average"},
   {name: "Python", difficulty: "moderate"},
   {name: "HTML", difficulty: "Easy"},
   {name: "CSS", difficulty: "Medium"},
   {name: "HTML", difficulty: "Effortless"}];

const sortedArray = languages.sort((a, b)=> {
  if (a.name === b.name){
   return a.difficulty < b.difficulty ? -1 : 1
  } else {
   return a.name < b.name ? -1 : 1
  }
})

console.log("Array of objects after sorting");
console.log(sortedArray);

Output

Sort Array of objects by two properties
[
  { name: 'CSS', difficulty: 'Medium' },
  { name: 'HTML', difficulty: 'Easy' },
  { name: 'HTML', difficulty: 'Effortless' },
  { name: 'Java', difficulty: 'Tough' },
  { name: 'Javascript', difficulty: 'Average' },
  { name: 'Javascript', difficulty: 'Medium' },
  { name: 'Python', difficulty: 'Easy' },
  { name: 'Python', difficulty: 'moderate' }
]

Complexity

In the above algorithm we have initially used a sort method of Javascript that takes O(n) time to sort the objects of length n followed by the if conditions that again takes up to the objects length time to sort the objects. So after summing both: O(n) + O(n) = O(2n). In O(2n) 2 is constant, and in complexity constant does not matter, hence the result will be O(n) time. The space complexity taken is O(n).

Conclusion

This is how we can solve the given problem and logical thinking is key to get an idea about solving any problem. In the context of coding going from simple sort javascript one line method for the particular problem statement in sorting the array of objects..

Updated on: 18-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements