20 lines
554 B
TypeScript
20 lines
554 B
TypeScript
export type SortDir = 'ASC' | 'DESC';
|
|
|
|
/**
|
|
* Returns a new array of documents sorted by documentDate.
|
|
* Documents without a date are always placed last, regardless of direction.
|
|
*/
|
|
export function sortDocumentsByDate<T extends { documentDate?: string | null }>(
|
|
docs: T[],
|
|
dir: SortDir
|
|
): T[] {
|
|
return [...docs].sort((a, b) => {
|
|
const da = a.documentDate ?? '';
|
|
const db = b.documentDate ?? '';
|
|
if (!da && !db) return 0;
|
|
if (!da) return 1;
|
|
if (!db) return -1;
|
|
return dir === 'DESC' ? db.localeCompare(da) : da.localeCompare(db);
|
|
});
|
|
}
|