#200 - media file sorting fix

This commit is contained in:
Elio Struyf
2021-12-05 13:11:22 +01:00
parent 15a6ea8d8d
commit 2bac596e3d
3 changed files with 27 additions and 1 deletions

View File

@@ -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

View File

@@ -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"));
}

View File

@@ -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