harwind

harwind

JavaScript Array of Objects Sorting: How to Sort by a Specific Object Property

I have an array of objects in JavaScript, and I want to sort them based on a specific property of the objects. For example, I have an array of books, and each book object has properties like ‘title’, ‘author’, and ‘publicationYear’. How can I sort this array of book objects alphabetically by the ‘title’ property?

Here’s a simplified version of the array:

const books = [
    { title: 'The Great Gatsby', author: 'F. Scott Fitzgerald', publicationYear: 1925 },
    { title: 'To Kill a Mockingbird', author: 'Harper Lee', publicationYear: 1960 },
    { title: '1984', author: 'George Orwell', publicationYear: 1949 }
];

This array should be sorted ascendingly using the ‘title’ parameter. I attempted to discover the answer by going to several sites like Scaler, but I was unable to locate the solution. Could you supply a JavaScript code sample that explains how to do this sorting and explains any key ideas or functions utilized in the code? I appreciate your help.

/js

Most Liked

gulshan212

gulshan212

Well, here is a simple example of sorting.

const books = [
    { title: 'The Great Gatsby', author: 'F. Scott Fitzgerald', publicationYear: 1925 },
    { title: 'To Kill a Mockingbird', author: 'Harper Lee', publicationYear: 1960 },
    { title: '1984', author: 'George Orwell', publicationYear: 1949 }
];

// Sort the books array based on the 'title' property
books.sort((a, b) => {
    const titleA = a.title.toLowerCase(); // Convert titles to lowercase for case-insensitive sorting
    const titleB = b.title.toLowerCase();

    if (titleA < titleB) {
        return -1; // 'a' should come before 'b'
    } else if (titleA > titleB) {
        return 1; // 'b' should come before 'a'
    } else {
        return 0; // Titles are equal, no change in order
    }
});

// Now, the books array is sorted alphabetically by title
console.log(books);

Thanks

kokolegorille

kokolegorille

A one liner…

books.sort((a, b) => a.title.toLowerCase() < b.title.toLowerCase() ? -1 : 1)

Popular General Dev topics Top

AstonJ
This article got me thinking about encrypted chat: Europol said that French police had discovered some of EncroChat’s servers were lo...
New
Kurisu
You can go directly to the last paragraph of this post to read about my concern. I was trying Git submodules then found the above po...
New
dimitarvp
What does a developer advocate do for a living? I mean, what is it that you are paid to do? I’ve seen your description below but it doesn...
New
AstonJ
Stopwords are words that you normally filter for things like search queries, such as ‘as’, ‘because’ etc - there are a few online, but I ...
New
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
PaulMartin
Hey everyone! Do you have any tips or free resources that can help me learn Rspec? Although I know how to write some Rspec, I’m not very...
New
harwind
I have an array of objects in JavaScript, and I want to sort them based on a specific property of the objects. For example, I have an arr...
/js
New
thetoaderseventytwo
I’ve been trying to dip my feet into using Unity and C# for the sake of developing games, however, I have barely any knowledge of how to ...
New
jaeyson
Is Rust still good to learn? Last time (ages ago) I heard there was changes made by the foundation. If not, is Go suitable for api and w...
New

Other popular topics Top

Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
PragmaticBookshelf
Design and develop sophisticated 2D games that are as much fun to make as they are to play. From particle effects and pathfinding to soci...
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
dimitarvp
Small essay with thoughts on macOS vs. Linux: I know @Exadra37 is just waiting around the corner to scream at me “I TOLD YOU SO!!!” but I...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
New
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New
PragmaticBookshelf
A Ruby-Centric Chat with Noel Rappin @noelrappin Once you start noodling around with Ruby you quickly figure out, as Noel Rappi...
New