Well, here is a simple example of sorting.
const books = [
{ title: 'The Great Gatsby', author: 'F. Scott Fitzgerald', publicationYear: 1925 },
{ title: 'To Kill a Mockingbird', author: 'Harper Lee', publicationYear: 1960 },
{ title: '1984', author: 'George Orwell', publicationYear: 1949 }
];
// Sort the books array based on the 'title' property
books.sort((a, b) => {
const titleA = a.title.toLowerCase(); // Convert titles to lowercase for case-insensitive sorting
const titleB = b.title.toLowerCase();
if (titleA < titleB) {
return -1; // 'a' should come before 'b'
} else if (titleA > titleB) {
return 1; // 'b' should come before 'a'
} else {
return 0; // Titles are equal, no change in order
}
});
// Now, the books array is sorted alphabetically by title
console.log(books);
Thanks