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)

Where Next?

Popular General Dev topics Top

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
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
sona11
How can I apply a modified date and time to a variable? This is what I get when I execute the following query in SQL Server Mgmt Studio: ...
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
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
harwind
I’m working on a SQL query for a database containing records of customer transactions. Each transaction has a transaction_id, customer_id...
New
harwind
Given an array of integers, find the length of the longest increasing subsequence. A subsequence is a sequence that can be derived from a...
New
Tazmeen
Hello, I am new to this forum. Not really sure if this topic is relevant for this chat at all. I apologize if its not. I am trying to c...
New
GTX
Hello everyone! I am not a developer, just wanna know if it’s possible for someone with no skills to learn how to reverse hack my hackers.
New

Other popular topics Top

ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
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
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
New
PragmaticBookshelf
Author Spotlight: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
New
Fl4m3Ph03n1x
Background Lately I am in a quest to find a good quality TTS ai generation tool to run locally in order to create audio for some videos I...
New