From 7196311bb30a08d6c9ef46506fc3e121d8c3c00d Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 13 Mar 2026 20:57:10 +0200 Subject: Sort books by publication year --- js/app.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'js') diff --git a/js/app.js b/js/app.js index 0a563a5..a8602f0 100644 --- a/js/app.js +++ b/js/app.js @@ -51,7 +51,7 @@ async function loadBooks() { const response = await fetch('data/books.json'); if (!response.ok) throw new Error('Failed to load books'); const data = await response.json(); - state.books = data.books; + state.books = sortBooksByYear(data.books); state.filteredBooks = [...state.books]; populateAuthorFilter(); } catch (error) { @@ -64,6 +64,17 @@ async function loadBooks() { } } +function sortBooksByYear(books) { + return [...books].sort((a, b) => { + if (a.year !== b.year) return a.year - b.year; + + const authorCompare = a.author.localeCompare(b.author); + if (authorCompare !== 0) return authorCompare; + + return a.title.localeCompare(b.title); + }); +} + // Populate author filter dropdown with unique authors function populateAuthorFilter() { const authors = [...new Set(state.books.map(book => book.author))].sort(); -- cgit v1.2.3