mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-06-23 16:05:09 +00:00
47 lines
1.6 KiB
TypeScript
47 lines
1.6 KiB
TypeScript
|
|
import { PdfFile } from '../../wrappers/PdfFile';
|
|
|
|
export async function sortPdfArray(
|
|
files: PdfFile[],
|
|
sortType: "orderProvided"|"byFileName"|"byDateModified"|"byDateCreated"|"byPDFTitle" = "orderProvided"
|
|
): Promise<PdfFile[]> {
|
|
|
|
const docCache = await PdfFile.cacheAsPdfLibDocuments(files);
|
|
|
|
switch(sortType) {
|
|
case "byFileName":
|
|
files.sort((a, b) => {
|
|
if (!a || !b) return 0;
|
|
const ad = a.filename, bd = b.filename;
|
|
if (!ad || !bd) return 0;
|
|
return ad.localeCompare(bd);
|
|
});
|
|
break;
|
|
case "byDateModified":
|
|
files.sort((a, b) => {
|
|
const ad = docCache.get(a)?.getModificationDate()?.getTime();
|
|
const bd = docCache.get(b)?.getModificationDate()?.getTime();
|
|
if (!ad || !bd) return 0;
|
|
return ad > bd ? 1 : -1
|
|
});
|
|
break;
|
|
case "byDateCreated":
|
|
files.sort((a, b) => {
|
|
const ad = docCache.get(a)?.getCreationDate()?.getTime();
|
|
const bd = docCache.get(b)?.getCreationDate()?.getTime();
|
|
if (!ad || !bd) return 0;
|
|
return ad > bd ? 1 : -1
|
|
});
|
|
break;
|
|
case "byPDFTitle":
|
|
files.sort((a, b) => {
|
|
const ad = docCache.get(a)?.getTitle();
|
|
const bd = docCache.get(b)?.getTitle();
|
|
if (!ad || !bd) return 0;
|
|
return ad.localeCompare(bd);
|
|
});
|
|
break;
|
|
}
|
|
|
|
return files;
|
|
} |