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
The obligatory speed test thread :smiley: Check here: https://www.speedtest.net When complete, click on the share link and copy and pas...
New
AstonJ
A thread that every forum needs! Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New
AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
AstonJ
Which apps do you think are killing it right now? Either from a technical perspective or ones that you like personally or feel have been...
New
foxtrottwist
Here’s our thread for the Keyboardio Atreus. It is a mechanical keyboard based on and a slight update of the original Atreus (Keyboardio ...
New
Exadra37
I am a Linux user since 2012, more or less, and I always use Ubuntu on my computers, and my last 2 laptops have been used Thinkpads, wher...
New
mafinar
I always start with excitement and then get busy on 9/10th day. This year, like the year before this, and the year before that, I intend ...
New
New
First poster: mindriot
LG 28-inch 16:18 DualUp Monitor with Ergo Stand and USB Type-C™ (28MQ780-B) | LG USA. Shop LG 28MQ780-B on the official LG.com website ...
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New

Other popular topics Top

AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
PragmaticBookshelf
A Hero’s Journey with Chris Pine @chrispine Chris Pine, author of Learn to Program, Third Edition, discusses his journey to beco...
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File &gt; New Rule: And select Deny, O...
New