Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Challenge: Sort and Extract Data with sort() | Advanced Array Methods and Transformations
JavaScript Data Structures

book
Challenge: Sort and Extract Data with sort()

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.

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);
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:

python
Sorted by Title (Ascending): Noughts & Crosses, Priestdaddy, The Cost of Living
Sorted by Author (Descending): Patricia Lockwood, Malorie Blackman, Deborah Levy
Sorted by Year (Descending): 2018, 2017, 2001
  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.

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);
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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 5. Capitolo 8

Chieda ad AI

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

some-alt