Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Sorting and Extracting Properties | Advanced Array Operations
JavaScript Data Structures
course content

Course Content

JavaScript Data Structures

JavaScript Data Structures

1. Introduction and Prerequisites
2. Objects Fundamentals
3. Advanced Object Manipulation
4. Mastering Arrays
5. Advanced Array Operations

bookChallenge: Sorting and Extracting Properties

Task

Given an array of books, each represented by an object with properties (title, author, publicationYear), create a solution that performs the following tasks:

  1. Sort the array of books:
    • Sort by title in ascending order;
    • Sort by author in descending order;
    • Sort by year in descending order.
  2. Extract specific properties into separate arrays:
    • Create an array containing only the titles of books (sorted by title in ascending order);
    • Create an array containing only the authors of books (sorted by author in descending order);
    • Create an array containing only the publication years of books (sorted by year in descending order).

Ensure that the original array of books remains unaltered.

123456789101112131415161718192021222324252627282930313233343536
const books = [ { title: "Noughts & Crosses", author: "Malorie Blackman", publicationYear: 2001, }, { title: "Priestdaddy", author: "Patricia Lockwood", publicationYear: 2017, }, { title: "The Cost of Living", author: "Deborah Levy", publicationYear: 2018, }, ]; // Sort by `title` in ascending order const sortedByTitleAscending = ___ .sort((a, b) => a.title.___(b.title)) .___((book) => book.title); // Sort by `author` in descending order const sortedByAuthorDescending = [...books] .___((a, b) => b.___.localeCompare(a.___)) .map((book) => book.author); // Sort by `year` in descending order const sortedByYearDescending = [...books] .sort((a, b) => ___ ___ ___) .___((book) => book.publicationYear); console.log("Sorted by Title (Ascending):", sortedByTitleAscending); console.log("Sorted by Author (Descending):", sortedByAuthorDescending); console.log("Sorted by Year (Descending):", sortedByYearDescending);
copy

Expected output:

  1. For sorting by title, utilize localeCompare() with the title property.
  2. For sorting by author, apply localeCompare() with the author property.
  3. For sorting by year, use a numeric comparison based on the publicationYear property.
  4. Utilize the map() method to create new arrays with specific properties.
  5. Create a callback function for map() that returns the desired property for each book.
  6. For extracting titles, authors, and years, the callback functions should return the titleauthor, and publicationYear properties, respectively.
  7. Ensure that the original array of books remains unaltered. Use the spread ([...books]) syntax to create a copy for sorting and extracting.
123456789101112131415161718192021222324252627282930313233343536
const books = [ { title: "Noughts & Crosses", author: "Malorie Blackman", publicationYear: 2001, }, { title: "Priestdaddy", author: "Patricia Lockwood", publicationYear: 2017, }, { title: "The Cost of Living", author: "Deborah Levy", publicationYear: 2018, }, ]; // Sort by `title` in ascending order const sortedByTitleAscending = [...books] .sort((a, b) => a.title.localeCompare(b.title)) .map((book) => book.title); // Sort by `author` in descending order const sortedByAuthorDescending = [...books] .sort((a, b) => b.author.localeCompare(a.author)) .map((book) => book.author); // Sort by `year` in descending order const sortedByYearDescending = [...books] .sort((a, b) => b.publicationYear - a.publicationYear) .map((book) => book.publicationYear); console.log("Sorted by Title (Ascending):", sortedByTitleAscending); console.log("Sorted by Author (Descending):", sortedByAuthorDescending); console.log("Sorted by Year (Descending):", sortedByYearDescending);
copy

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 8
We're sorry to hear that something went wrong. What happened?
some-alt