diff --git a/src/components/PositiveNewsFeedPanel.ts b/src/components/PositiveNewsFeedPanel.ts index 4a9cc9d31..c00d1c3a1 100644 --- a/src/components/PositiveNewsFeedPanel.ts +++ b/src/components/PositiveNewsFeedPanel.ts @@ -102,14 +102,27 @@ export class PositiveNewsFeedPanel extends Panel { * Attaches a delegated click handler for share buttons. */ private renderCards(items: NewsItem[]): void { - this.filteredItems = items; - - if (items.length === 0) { + // Sort by publication date descending (newest first). + // Items with missing or invalid dates are placed at the end. + const sorted = [...items].sort((a, b) => { + const ta = a.pubDate ? new Date(a.pubDate).getTime() : NaN; + const tb = b.pubDate ? new Date(b.pubDate).getTime() : NaN; + const aValid = !isNaN(ta); + const bValid = !isNaN(tb); + if (aValid && bValid) return tb - ta; + if (aValid) return -1; + if (bValid) return 1; + return 0; + }); + + this.filteredItems = sorted; + + if (sorted.length === 0) { this.content.innerHTML = '
No stories in this category yet
'; return; } - this.content.innerHTML = items.map((item, idx) => this.renderCard(item, idx)).join(''); + this.content.innerHTML = sorted.map((item, idx) => this.renderCard(item, idx)).join(''); // Delegated click handler for share buttons (remove first to avoid stacking) this.content.removeEventListener('click', this.handleShareClick);