Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Sfida: Ordina ed Estrai Dati con sort() | Metodi Avanzati degli Array e Trasformazioni
Strutture Dati JavaScript

bookSfida: 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:

  1. Ordinare l'array di libri:
    • Ordinare per titolo in ordine crescente;
    • Ordinare per autore in ordine decrescente;
    • Ordinare per anno in ordine decrescente.
  2. 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.

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

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
  1. Per l'ordinamento per titolo, utilizzare localeCompare() con la proprietà title.
  2. Per l'ordinamento per autore, applicare localeCompare() con la proprietà author.
  3. Per l'ordinamento per anno, utilizzare un confronto numerico basato sulla proprietà publicationYear.
  4. Utilizzare il metodo map() per creare nuovi array con proprietà specifiche.
  5. Creare una funzione di callback per map() che restituisca la proprietà desiderata per ciascun libro.
  6. Per estrarre titoli, autori e anni, le funzioni di callback devono restituire rispettivamente le proprietà titleauthor e publicationYear.
  7. Assicurarsi che l'array originale dei libri rimanga invariato. Utilizzare la sintassi spread ([...books]) per creare una copia da ordinare ed estrarre.
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

Chieda ad AI

ChatGPT

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

Suggested prompts:

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

bookSfida: 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:

  1. Ordinare l'array di libri:
    • Ordinare per titolo in ordine crescente;
    • Ordinare per autore in ordine decrescente;
    • Ordinare per anno in ordine decrescente.
  2. 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.

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

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
  1. Per l'ordinamento per titolo, utilizzare localeCompare() con la proprietà title.
  2. Per l'ordinamento per autore, applicare localeCompare() con la proprietà author.
  3. Per l'ordinamento per anno, utilizzare un confronto numerico basato sulla proprietà publicationYear.
  4. Utilizzare il metodo map() per creare nuovi array con proprietà specifiche.
  5. Creare una funzione di callback per map() che restituisca la proprietà desiderata per ciascun libro.
  6. Per estrarre titoli, autori e anni, le funzioni di callback devono restituire rispettivamente le proprietà titleauthor e publicationYear.
  7. Assicurarsi che l'array originale dei libri rimanga invariato. Utilizzare la sintassi spread ([...books]) per creare una copia da ordinare ed estrarre.
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
some-alt