How to sort array of objects in JavaScript
Understanding Arrays and Objects
Before we delve into the core concept of sorting arrays and objects in JavaScript, let's briefly understand what arrays and objects are in the context of programming. Visualize an array as a list that contains items, and each item has a specific order or position in that list. An object, on the other hand, can be likened to a box where you can store different types of items, and each item has a unique label to identify it.
Now, imagine having an array of objects, which is like a list of boxes. Each box can contain different items, and each item has a unique label. The challenge now is how to organize these list of boxes (array of objects) based on the labels on the items they contain. That's what we are going to learn today.
The Basics of Sorting in JavaScript
Sorting is essentially arranging items in a specific order. In JavaScript, the sort()
method is commonly used to sort arrays.
Here's an example:
let numbers = [5, 2, 9, 1, 5, 6];
numbers.sort();
console.log(numbers); // [1, 2, 5, 5, 6, 9]
The sort()
method does a great job with sorting numbers or strings in an array. But when it comes to sorting an array of objects based on a specific property value, we need a more sophisticated approach.
Sorting an Array of Objects
In JavaScript, you can customize the sort()
method to sort an array of objects. This is done by passing a function to the sort()
method. This function, often called a compare function, tells JavaScript how to sort the array elements.
Let's create an array of objects, where each object represents a book with a title and an author.
let books = [
{ title: 'Harry Potter', author: 'J.K. Rowling' },
{ title: 'The Hobbit', author: 'J.R.R. Tolkien' },
{ title: '1984', author: 'George Orwell' },
];
Now, let's say we want to sort these books alphabetically by the title.
books.sort(function (a, b) {
if (a.title < b.title) {
return -1;
}
if (a.title > b.title) {
return 1;
}
return 0;
});
console.log(books);
In the code above, the compare function takes two arguments, a
and b
, which represent two elements in the array. The function compares the title
property of both elements and returns -1
if a
should come before b
, 1
if a
should come after b
, and 0
if they are equal. The sort()
method uses this function as a guide to arrange the elements in the array.
Simplifying with Arrow Functions
If you are familiar with ES6 arrow functions, the compare function can be written in a more concise way.
books.sort((a, b) => a.title.localeCompare(b.title));
The localeCompare()
method is a built-in JavaScript method that compares two strings and returns a negative, zero, or positive value depending on the sort order. This makes our code shorter and cleaner.
Sorting in Descending Order
To sort in descending order, you can simply reverse the positions of a
and b
in the compare function.
books.sort((a, b) => b.title.localeCompare(a.title));
Conclusion
Sorting an array of objects in JavaScript might seem a bit daunting at first, especially if you're new to programming. However, once you understand the logic behind the sort()
method and the compare function, it becomes a powerful tool in your JavaScript toolkit.
Remember, programming is like solving a puzzle. Each piece might not make sense on its own, but when you start putting them together, you begin to see the whole picture. So, keep practicing, keep solving those puzzles, and soon, you'll be sorting arrays of objects like a pro!