diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-13 20:57:10 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-13 20:57:10 +0200 |
| commit | 7196311bb30a08d6c9ef46506fc3e121d8c3c00d (patch) | |
| tree | 00058b53a4acdb83688a544a3ce2d9ccf98c54ea | |
| parent | 37907093fd0d3b954d07579e13fd7b233c2b1080 (diff) | |
Sort books by publication year
| -rw-r--r-- | js/app.js | 13 |
1 files changed, 12 insertions, 1 deletions
@@ -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(); |
