diff options
-rw-r--r-- | viewer/ldgallery-viewer.7.md | 3 | ||||
-rw-r--r-- | viewer/src/services/itemComparators.ts | 33 |
2 files changed, 30 insertions, 6 deletions
diff --git a/viewer/ldgallery-viewer.7.md b/viewer/ldgallery-viewer.7.md index 9ac1512..6b31879 100644 --- a/viewer/ldgallery-viewer.7.md +++ b/viewer/ldgallery-viewer.7.md | |||
@@ -2,7 +2,7 @@ | |||
2 | pagetitle: Viewer user manual - ldgallery | 2 | pagetitle: Viewer user manual - ldgallery |
3 | title: LDGALLERY-VIEWER(7) ldgallery | 3 | title: LDGALLERY-VIEWER(7) ldgallery |
4 | author: Pacien TRAN-GIRARD, Guillaume FOUET | 4 | author: Pacien TRAN-GIRARD, Guillaume FOUET |
5 | date: 2020-09-19 (v2.0) | 5 | date: 2020-09-24 (v2.0) |
6 | --- | 6 | --- |
7 | 7 | ||
8 | 8 | ||
@@ -79,6 +79,7 @@ initialItemSort | |||
79 | Possible values are "title_asc", "date_asc", "date_desc". | 79 | Possible values are "title_asc", "date_asc", "date_desc". |
80 | Defaults to "title_asc". | 80 | Defaults to "title_asc". |
81 | Titles are sorted using a human-friendly _natural sort order_ which treats multi-digit numbers atomically. | 81 | Titles are sorted using a human-friendly _natural sort order_ which treats multi-digit numbers atomically. |
82 | The item path is used as a tie-breaker for all the defined orders. | ||
82 | 83 | ||
83 | <!-- https://github.com/pacien/ldgallery/issues/27 | 84 | <!-- https://github.com/pacien/ldgallery/issues/27 |
84 | initialSearchQuery | 85 | initialSearchQuery |
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 | |||
23 | export type ItemSort = { name: Gallery.ItemSortStr; text: TranslateResult; fn: ItemComparator }; | 23 | export type ItemSort = { name: Gallery.ItemSortStr; text: TranslateResult; fn: ItemComparator }; |
24 | 24 | ||
25 | export default class ItemComparators { | 25 | export default class ItemComparators { |
26 | static readonly DEFAULT = ItemComparators.sortByTitleAsc; | ||
27 | |||
28 | static readonly ITEM_SORTS: ItemSort[] = [ | 26 | static readonly ITEM_SORTS: ItemSort[] = [ |
29 | { name: "title_asc", text: i18n.t("command.sort.byTitleAsc"), fn: ItemComparators.sortByTitleAsc }, | 27 | { |
30 | { name: "date_asc", text: i18n.t("command.sort.byDateAsc"), fn: ItemComparators.sortByDateAsc }, | 28 | name: "title_asc", |
29 | text: i18n.t("command.sort.byTitleAsc"), | ||
30 | fn: ItemComparators.chain(ItemComparators.sortByTitleAsc, ItemComparators.sortByPathAsc), | ||
31 | }, | ||
32 | { | ||
33 | name: "date_asc", | ||
34 | text: i18n.t("command.sort.byDateAsc"), | ||
35 | fn: ItemComparators.chain(ItemComparators.sortByDateAsc, ItemComparators.sortByPathAsc), | ||
36 | }, | ||
31 | { | 37 | { |
32 | name: "date_desc", | 38 | name: "date_desc", |
33 | text: i18n.t("command.sort.byDateDesc"), | 39 | text: i18n.t("command.sort.byDateDesc"), |
34 | fn: ItemComparators.reverse(ItemComparators.sortByDateAsc), | 40 | fn: ItemComparators.reverse(ItemComparators.chain(ItemComparators.sortByDateAsc, ItemComparators.sortByPathAsc)), |
35 | }, | 41 | }, |
36 | ]; | 42 | ]; |
37 | 43 | ||
44 | static readonly DEFAULT = ItemComparators.ITEM_SORTS[0].fn; | ||
45 | |||
46 | static sortByPathAsc(left: Gallery.Item, right: Gallery.Item): number { | ||
47 | return left.path.localeCompare(right.path, undefined, { | ||
48 | sensitivity: "base", | ||
49 | ignorePunctuation: true, | ||
50 | numeric: true, | ||
51 | }); | ||
52 | } | ||
53 | |||
38 | static sortByTitleAsc(left: Gallery.Item, right: Gallery.Item): number { | 54 | static sortByTitleAsc(left: Gallery.Item, right: Gallery.Item): number { |
39 | return left.title.localeCompare(right.title, undefined, { | 55 | return left.title.localeCompare(right.title, undefined, { |
40 | sensitivity: "base", | 56 | sensitivity: "base", |
@@ -50,4 +66,11 @@ export default class ItemComparators { | |||
50 | static reverse(fn: ItemComparator): ItemComparator { | 66 | static reverse(fn: ItemComparator): ItemComparator { |
51 | return (l, r) => -fn(l, r); | 67 | return (l, r) => -fn(l, r); |
52 | } | 68 | } |
69 | |||
70 | static chain(primary: ItemComparator, tieBreaker: ItemComparator): ItemComparator { | ||
71 | return (l, r) => { | ||
72 | const primaryComparison = primary(l, r); | ||
73 | return primaryComparison !== 0 ? primaryComparison : tieBreaker(l, r); | ||
74 | }; | ||
75 | } | ||
53 | } | 76 | } |