From f8a6abf3913ce83c0518ab642da3c06124fe6aa5 Mon Sep 17 00:00:00 2001 From: Tomas Dvorak Date: Sat, 14 Mar 2026 11:21:31 +0100 Subject: [PATCH] Fix blog sorting to show newest first - When files have recent timestamps (from setup script), sort by numeric ID - Higher numeric IDs represent newer blogs - This ensures proper chronological ordering in admin interface --- backend/main.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/backend/main.go b/backend/main.go index e3c0810..16eb3e8 100644 --- a/backend/main.go +++ b/backend/main.go @@ -658,13 +658,33 @@ func listLatestBlogs(siteRoot string, limit int) ([]BlogItem, error) { }) } sort.Slice(items, func(i, j int) bool { - // Descending by mod time, fallback to numeric ID desc + // Check if files were recently processed (all have same timestamp from setup script) + recentThreshold := time.Now().Add(-24 * time.Hour) + allRecent := items[i].MTime.After(recentThreshold) && items[j].MTime.After(recentThreshold) + + if allRecent { + // If both files are recent (from setup script), sort by numeric ID (higher = newer) + ii, err1 := strconv.Atoi(items[i].ID) + jj, err2 := strconv.Atoi(items[j].ID) + if err1 == nil && err2 == nil { + return ii > jj + } + // If not numeric, fall back to string comparison + return items[i].ID > items[j].ID + } + + // Otherwise, use modification time (newest first) if !items[i].MTime.Equal(items[j].MTime) { return items[i].MTime.After(items[j].MTime) } - ii, _ := strconv.Atoi(items[i].ID) - jj, _ := strconv.Atoi(items[j].ID) - return ii > jj + + // If times are equal and not recent, fallback to numeric ID + ii, err1 := strconv.Atoi(items[i].ID) + jj, err2 := strconv.Atoi(items[j].ID) + if err1 == nil && err2 == nil { + return ii > jj + } + return items[i].ID > items[j].ID }) if limit > 0 && len(items) > limit { items = items[:limit]