Défi : Trier et Extraire des Données avec sort()
Tâche
Étant donné un tableau de livres, chacun représenté par un objet avec les propriétés (title, author, publicationYear), élaborer une solution qui réalise les opérations suivantes :
- Trier le tableau de livres :
- Trier par titre par ordre croissant ;
- Trier par auteur par ordre décroissant ;
- Trier par année de publication par ordre décroissant.
- Extraire des propriétés spécifiques dans des tableaux séparés :
- Créer un tableau contenant uniquement les titres des livres (triés par titre par ordre croissant) ;
- Créer un tableau contenant uniquement les auteurs des livres (triés par auteur par ordre décroissant) ;
- Créer un tableau contenant uniquement les années de publication des livres (triées par année par ordre décroissant).
Veiller à ce que le tableau original de livres reste inchangé.
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);
Résultat attendu :
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
- Pour le tri par titre, utiliser
localeCompare()avec la propriététitle. - Pour le tri par auteur, appliquer
localeCompare()avec la propriétéauthor. - Pour le tri par année, utiliser une comparaison numérique basée sur la propriété
publicationYear. - Utiliser la méthode
map()pour créer de nouveaux tableaux avec des propriétés spécifiques. - Créer une fonction de rappel pour
map()qui retourne la propriété souhaitée pour chaque livre. - Pour extraire les titres, auteurs et années, les fonctions de rappel doivent retourner respectivement les propriétés
title,authoretpublicationYear. - S'assurer que le tableau original de livres reste inchangé. Utiliser la syntaxe spread (
[...books]) pour créer une copie destinée au tri et à l'extraction.
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);
Tout était clair ?
Merci pour vos commentaires !
Section 5. Chapitre 8
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Awesome!
Completion rate improved to 2.27
Défi : Trier et Extraire des Données avec sort()
Glissez pour afficher le menu
Tâche
Étant donné un tableau de livres, chacun représenté par un objet avec les propriétés (title, author, publicationYear), élaborer une solution qui réalise les opérations suivantes :
- Trier le tableau de livres :
- Trier par titre par ordre croissant ;
- Trier par auteur par ordre décroissant ;
- Trier par année de publication par ordre décroissant.
- Extraire des propriétés spécifiques dans des tableaux séparés :
- Créer un tableau contenant uniquement les titres des livres (triés par titre par ordre croissant) ;
- Créer un tableau contenant uniquement les auteurs des livres (triés par auteur par ordre décroissant) ;
- Créer un tableau contenant uniquement les années de publication des livres (triées par année par ordre décroissant).
Veiller à ce que le tableau original de livres reste inchangé.
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);
Résultat attendu :
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
- Pour le tri par titre, utiliser
localeCompare()avec la propriététitle. - Pour le tri par auteur, appliquer
localeCompare()avec la propriétéauthor. - Pour le tri par année, utiliser une comparaison numérique basée sur la propriété
publicationYear. - Utiliser la méthode
map()pour créer de nouveaux tableaux avec des propriétés spécifiques. - Créer une fonction de rappel pour
map()qui retourne la propriété souhaitée pour chaque livre. - Pour extraire les titres, auteurs et années, les fonctions de rappel doivent retourner respectivement les propriétés
title,authoretpublicationYear. - S'assurer que le tableau original de livres reste inchangé. Utiliser la syntaxe spread (
[...books]) pour créer une copie destinée au tri et à l'extraction.
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);
Tout était clair ?
Merci pour vos commentaires !
Section 5. Chapitre 8