summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-13 20:57:10 +0200
committerPaul Buetow <paul@buetow.org>2026-03-13 20:57:10 +0200
commit7196311bb30a08d6c9ef46506fc3e121d8c3c00d (patch)
tree00058b53a4acdb83688a544a3ce2d9ccf98c54ea
parent37907093fd0d3b954d07579e13fd7b233c2b1080 (diff)
Sort books by publication year
-rw-r--r--js/app.js13
1 files changed, 12 insertions, 1 deletions
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();