From c108a99e128c02d20930775222f6118a460f80b1 Mon Sep 17 00:00:00 2001 From: pacien Date: Thu, 24 Sep 2020 22:18:44 +0200 Subject: viewer/services/itemComparators: make the orders linear By using the item path as a tie-breaker. GitHub: closes #255 --- viewer/src/services/itemComparators.ts | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'viewer/src/services') diff --git a/viewer/src/services/itemComparators.ts b/viewer/src/services/itemComparators.ts index e131b8b..ab1036d 100644 --- a/viewer/src/services/itemComparators.ts +++ b/viewer/src/services/itemComparators.ts @@ -23,18 +23,34 @@ export type ItemComparator = (left: Gallery.Item, right: Gallery.Item) => number export type ItemSort = { name: Gallery.ItemSortStr; text: TranslateResult; fn: ItemComparator }; export default class ItemComparators { - static readonly DEFAULT = ItemComparators.sortByTitleAsc; - static readonly ITEM_SORTS: ItemSort[] = [ - { name: "title_asc", text: i18n.t("command.sort.byTitleAsc"), fn: ItemComparators.sortByTitleAsc }, - { name: "date_asc", text: i18n.t("command.sort.byDateAsc"), fn: ItemComparators.sortByDateAsc }, + { + name: "title_asc", + text: i18n.t("command.sort.byTitleAsc"), + fn: ItemComparators.chain(ItemComparators.sortByTitleAsc, ItemComparators.sortByPathAsc), + }, + { + name: "date_asc", + text: i18n.t("command.sort.byDateAsc"), + fn: ItemComparators.chain(ItemComparators.sortByDateAsc, ItemComparators.sortByPathAsc), + }, { name: "date_desc", text: i18n.t("command.sort.byDateDesc"), - fn: ItemComparators.reverse(ItemComparators.sortByDateAsc), + fn: ItemComparators.reverse(ItemComparators.chain(ItemComparators.sortByDateAsc, ItemComparators.sortByPathAsc)), }, ]; + static readonly DEFAULT = ItemComparators.ITEM_SORTS[0].fn; + + static sortByPathAsc(left: Gallery.Item, right: Gallery.Item): number { + return left.path.localeCompare(right.path, undefined, { + sensitivity: "base", + ignorePunctuation: true, + numeric: true, + }); + } + static sortByTitleAsc(left: Gallery.Item, right: Gallery.Item): number { return left.title.localeCompare(right.title, undefined, { sensitivity: "base", @@ -50,4 +66,11 @@ export default class ItemComparators { static reverse(fn: ItemComparator): ItemComparator { return (l, r) => -fn(l, r); } + + static chain(primary: ItemComparator, tieBreaker: ItemComparator): ItemComparator { + return (l, r) => { + const primaryComparison = primary(l, r); + return primaryComparison !== 0 ? primaryComparison : tieBreaker(l, r); + }; + } } -- cgit v1.2.3