Sfida: Ordina ed Estrai Dati con sort()
Compito
Dato un array di libri, ciascuno rappresentato da un oggetto con proprietà (title, author, publicationYear), creare una soluzione che esegua le seguenti operazioni:
- Ordinare l'array di libri:
- Ordinare per titolo in ordine crescente;
- Ordinare per autore in ordine decrescente;
- Ordinare per anno in ordine decrescente.
- Estrarre proprietà specifiche in array separati:
- Creare un array contenente solo i titoli dei libri (ordinati per titolo in ordine crescente);
- Creare un array contenente solo gli autori dei libri (ordinati per autore in ordine decrescente);
- Creare un array contenente solo gli anni di pubblicazione dei libri (ordinati per anno in ordine decrescente).
Assicurarsi che l'array originale dei libri rimanga invariato.
123456789101112131415161718192021222324252627282930313233343536const 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);
Output atteso:
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
- Per l'ordinamento per titolo, utilizzare
localeCompare()con la proprietàtitle. - Per l'ordinamento per autore, applicare
localeCompare()con la proprietàauthor. - Per l'ordinamento per anno, utilizzare un confronto numerico basato sulla proprietà
publicationYear. - Utilizzare il metodo
map()per creare nuovi array con proprietà specifiche. - Creare una funzione di callback per
map()che restituisca la proprietà desiderata per ciascun libro. - Per estrarre titoli, autori e anni, le funzioni di callback devono restituire rispettivamente le proprietà
title,authorepublicationYear. - Assicurarsi che l'array originale dei libri rimanga invariato. Utilizzare la sintassi spread (
[...books]) per creare una copia da ordinare ed estrarre.
123456789101112131415161718192021222324252627282930313233343536const 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);
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Can you explain how the sorting functions work in this solution?
What would the output look like if the books array had more entries?
How can I modify this to sort by a different property, like title length?
Awesome!
Completion rate improved to 2.27
Sfida: Ordina ed Estrai Dati con sort()
Scorri per mostrare il menu
Compito
Dato un array di libri, ciascuno rappresentato da un oggetto con proprietà (title, author, publicationYear), creare una soluzione che esegua le seguenti operazioni:
- Ordinare l'array di libri:
- Ordinare per titolo in ordine crescente;
- Ordinare per autore in ordine decrescente;
- Ordinare per anno in ordine decrescente.
- Estrarre proprietà specifiche in array separati:
- Creare un array contenente solo i titoli dei libri (ordinati per titolo in ordine crescente);
- Creare un array contenente solo gli autori dei libri (ordinati per autore in ordine decrescente);
- Creare un array contenente solo gli anni di pubblicazione dei libri (ordinati per anno in ordine decrescente).
Assicurarsi che l'array originale dei libri rimanga invariato.
123456789101112131415161718192021222324252627282930313233343536const 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);
Output atteso:
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
- Per l'ordinamento per titolo, utilizzare
localeCompare()con la proprietàtitle. - Per l'ordinamento per autore, applicare
localeCompare()con la proprietàauthor. - Per l'ordinamento per anno, utilizzare un confronto numerico basato sulla proprietà
publicationYear. - Utilizzare il metodo
map()per creare nuovi array con proprietà specifiche. - Creare una funzione di callback per
map()che restituisca la proprietà desiderata per ciascun libro. - Per estrarre titoli, autori e anni, le funzioni di callback devono restituire rispettivamente le proprietà
title,authorepublicationYear. - Assicurarsi che l'array originale dei libri rimanga invariato. Utilizzare la sintassi spread (
[...books]) per creare una copia da ordinare ed estrarre.
123456789101112131415161718192021222324252627282930313233343536const 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);
Grazie per i tuoi commenti!