diff --git a/CHANGELOG.md b/CHANGELOG.md index 505920ae..8c191940 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ ### 🐞 Fixes - [#191](https://github.com/estruyf/vscode-front-matter/issues/191): Fix beta settings page +- [#200](https://github.com/estruyf/vscode-front-matter/issues/200): Fix last modified date sorting for media files - [#201](https://github.com/estruyf/vscode-front-matter/issues/201): Fix overflow issue with the media filename - [#202](https://github.com/estruyf/vscode-front-matter/issues/202): Fix checkbox label color for light themes diff --git a/src/commands/Dashboard.ts b/src/commands/Dashboard.ts index 0a458346..ffeacf11 100644 --- a/src/commands/Dashboard.ts +++ b/src/commands/Dashboard.ts @@ -434,7 +434,7 @@ export class Dashboard { if (crntSort?.type === SortType.string) { allMedia = allMedia.sort(Sorting.alphabetically("fsPath")); } else if (crntSort?.type === SortType.date) { - allMedia = allMedia.sort(Sorting.date("mtime")); + allMedia = allMedia.sort(Sorting.dateWithFallback("mtime", "fsPath")); } else { allMedia = allMedia.sort(Sorting.alphabetically("fsPath")); } diff --git a/src/helpers/Sorting.ts b/src/helpers/Sorting.ts index 20055e6a..fdd6de87 100644 --- a/src/helpers/Sorting.ts +++ b/src/helpers/Sorting.ts @@ -34,6 +34,31 @@ export class Sorting { }; }; + /** + * Sort by date with a fallback + * @param property + * @returns + */ + public static dateWithFallback = (property: string, fallback: string) => { + return (a: any, b: any) => { + const dateA = DateHelper.tryParse(a[property]); + const dateB = DateHelper.tryParse(b[property]); + + // Sort by date + var dCount = (dateA || new Date(0)).getTime() - (dateB || new Date(0)).getTime(); + if(dCount) return dCount; + + // If there is a tie, sort by fallback property + if (a[fallback] < b[fallback]) { + return -1; + } + if (a[fallback] > b[fallback]) { + return 1; + } + return 0; + }; + }; + /** * Sort by number * @param property