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

Discussion in 'App Development' started by Grinda21, Sep 18, 2023.

  1. Grinda21

    Grinda21

    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:

    Code:
    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 explain any key ideas or functions utilized in the code? I appreciate your help.
     
  2. 2rosy

    2rosy

  3. Whilst the stack overflow example is right on the money by showing a comparer function, that example is based on a numeric value.

    She had the example right on the page she posted on Scaler. It is the section:

    Sort an Array of Objects by Strings

    Someone that is not a developer cannot see the right way to do it even if you point them to an example, they need the exact code. Since the arrays are different I can tell that she still won't be able to do it.

     
  4. RantaMin

    RantaMin

    Using the 'sort()' method to sort an array of books by the 'title' property in alphabetical order, ascendingly.

    const books = [
    { title: 'The Catcher in the Rye', author: 'J.D. Salinger', publicationYear: 1951 },
    { title: 'To Kill a Mockingbird', author: 'Harper Lee', publicationYear: 1960 },
    { title: 'Pride and Prejudice', author: 'Jane Austen', publicationYear: 1813 }
    ];

    // Sorting the array by the 'title' property in ascending order
    books.sort((a, b) => {
    const titleA = a.title.toUpperCase();
    const titleB = b.title.toUpperCase();
    if (titleA < titleB) {
    return -1;
    }
    if (titleA > titleB) {
    return 1;
    }
    return 0;
    });

    // Displaying the sorted array in the console
    console.log(books);


    Also using the sort() method, and the comparison function employs localeCompare(). It's important to specify { sensitivity: 'base' } to ensure case-insensitive and special character-insensitive sorting.

    const books = [
    { title: 'War and Peace', author: 'Leo Tolstoy', publicationYear: 1869 },
    { title: 'Moby-Dick', author: 'Herman Melville', publicationYear: 1851 },
    { title: 'The Great Gatsby', author: 'F. Scott Fitzgerald', publicationYear: 1925 }
    ];

    // Sorting the array by the 'title' property in ascending order
    books.sort((a, b) => a.title.localeCompare(b.title, undefined, { sensitivity: 'base' }));

    // Displaying the sorted array in the console
    console.log(books);