Sort entries before storing

This commit is contained in:
Evan Fiordeliso 2025-06-29 18:43:23 -04:00
parent bd21890885
commit 976fe482b4
2 changed files with 9 additions and 5 deletions

View File

@ -31,7 +31,7 @@ export default class BookTrackerPlugin extends Plugin {
this.templater = new Templater(this.app); this.templater = new Templater(this.app);
this.storage = new Storage(this.app, this); this.storage = new Storage(this.app, this);
this.readingLog = new ReadingLog(this.app, this); this.readingLog = new ReadingLog(this);
this.addCommand({ this.addCommand({
id: "search-goodreads", id: "search-goodreads",

View File

@ -37,10 +37,7 @@ export class Storage {
export class ReadingLog { export class ReadingLog {
private entries: ReadingLogEntry[] = []; private entries: ReadingLogEntry[] = [];
public constructor( public constructor(private readonly plugin: BookTrackerPlugin) {
private readonly app: App,
private readonly plugin: BookTrackerPlugin
) {
this.loadEntries().catch((error) => { this.loadEntries().catch((error) => {
console.error("Failed to load reading log entries:", error); console.error("Failed to load reading log entries:", error);
}); });
@ -58,7 +55,14 @@ export class ReadingLog {
} }
} }
private sortEntries() {
this.entries = this.entries.sort(
(a, b) => b.createdAt.getTime() - a.createdAt.getTime()
);
}
private async storeEntries() { private async storeEntries() {
this.sortEntries();
await this.plugin.storage.writeJSON("reading-log.json", this.entries); await this.plugin.storage.writeJSON("reading-log.json", this.entries);
} }