Skip to content

Commit ed923e6

Browse files
authored
Merge pull request #28 from SebastianMC/27-support-true-alphabetical-order
#27 - support for true alphabetical order
2 parents 4bd3eaa + 742bdf1 commit ed923e6

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ sorting-spec: |
308308

309309
the line `... part \d+` says: group all notes and folders with name ending with 'part' followed by a number. Then order
310310
them by the number. And for clarity the subsequent (indented) line is added ` < a-z` which sets the order to
311-
alphanumerical ascending.
311+
alphabetical ascending.
312312

313313
The effect is:
314314

@@ -411,6 +411,35 @@ sorting-spec: |
411411
---
412412
```
413413

414+
## Alphabetical, Natural and True Alphabetical sorting orders
415+
416+
The 'A-Z' sorting (visible in Obsidian UI of file explorer) at some point before the 1.0.0 release of Obsidian actually became the so-called 'natural' sort order.
417+
For explanation of the term go to [Natural sort order](https://en.wikipedia.org/wiki/Natural_sort_order) on Wikipedia.
418+
The plugin follows the convention and the sorting specified by `< a-z` or `> a-z` triggers the _'natural sort order'_.
419+
420+
To allow the true alphabetical sort order, as suggested by the ticket [27: Not alphanumeric, but natural sort order?](https://github.com/SebastianMC/obsidian-custom-sort/issues/27)
421+
a distinct syntax was introduced: `< true a-z` and `> true a-z`
422+
423+
What is the difference?
424+
Using the example from the mentioned ticket: the items '0x01FF', '0x02FF' and '0x0200' sorted in _natural order_ go as:
425+
- 0x01FF -> the number 01 in the text is recognized
426+
- 0x02FF -> the number 02 in the text is recognized
427+
- 0x0200 -> the number 0200 in the text is recognized and it causes the third position of the item, because 0200 > 02
428+
429+
The same items when sorted in _true alphabetical_ order go as:
430+
- 0x01FF
431+
- 0x0200
432+
- 0x02FF -> the character 'F' following '2' goes after the character '0', that's why 0x02FF follows the 0x0200
433+
434+
You can use the order `< true a-z` or `> true a-z` to trigger the true alphabetical sorting, like in the ticket:
435+
```yaml
436+
sorting-spec: |
437+
target-folder: MaDo/...
438+
> true a-z
439+
target-folder: MaDo/Sandbox/SortingBug
440+
< true a-z
441+
```
442+
414443
## Location of sorting specification YAML entry
415444

416445
You can keep the custom sorting specifications in any of the following locations (or in all of them):

src/custom-sort/custom-sort-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ export enum CustomSortGroupType {
99

1010
export enum CustomSortOrder {
1111
alphabetical = 1, // = 1 to allow: if (customSortOrder) { ...
12+
trueAlphabetical,
1213
alphabeticalReverse,
14+
trueAlphabeticalReverse,
1315
byModifiedTime, // New to old
1416
byModifiedTimeAdvanced,
1517
byModifiedTimeReverse, // Old to new

src/custom-sort/custom-sort.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ let Collator = new Intl.Collator(undefined, {
88
numeric: true,
99
}).compare;
1010

11+
let CollatorTrueAlphabetical = new Intl.Collator(undefined, {
12+
usage: "sort",
13+
sensitivity: "base",
14+
numeric: false,
15+
}).compare;
16+
1117
export interface FolderItemForSorting {
1218
path: string
1319
groupIdx?: number // the index itself represents order for groups
@@ -24,7 +30,9 @@ type SorterFn = (a: FolderItemForSorting, b: FolderItemForSorting) => number
2430

2531
let Sorters: { [key in CustomSortOrder]: SorterFn } = {
2632
[CustomSortOrder.alphabetical]: (a: FolderItemForSorting, b: FolderItemForSorting) => Collator(a.sortString, b.sortString),
33+
[CustomSortOrder.trueAlphabetical]: (a: FolderItemForSorting, b: FolderItemForSorting) => CollatorTrueAlphabetical(a.sortString, b.sortString),
2734
[CustomSortOrder.alphabeticalReverse]: (a: FolderItemForSorting, b: FolderItemForSorting) => Collator(b.sortString, a.sortString),
35+
[CustomSortOrder.trueAlphabeticalReverse]: (a: FolderItemForSorting, b: FolderItemForSorting) => CollatorTrueAlphabetical(b.sortString, a.sortString),
2836
[CustomSortOrder.byModifiedTime]: (a: FolderItemForSorting, b: FolderItemForSorting) => (a.isFolder && b.isFolder) ? Collator(a.sortString, b.sortString) : (a.mtime - b.mtime),
2937
[CustomSortOrder.byModifiedTimeAdvanced]: (a: FolderItemForSorting, b: FolderItemForSorting) => a.mtime - b.mtime,
3038
[CustomSortOrder.byModifiedTimeReverse]: (a: FolderItemForSorting, b: FolderItemForSorting) => (a.isFolder && b.isFolder) ? Collator(a.sortString, b.sortString) : (b.mtime - a.mtime),

src/custom-sort/sorting-spec-processor.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,46 @@ describe('SortingSpecProcessor', () => {
436436
})
437437
})
438438

439+
const txtInputTrueAlphabeticalSortAttr: string = `
440+
target-folder: AAA
441+
< true a-z
442+
target-folder: BBB
443+
> true a-z
444+
`
445+
446+
const expectedSortSpecForTrueAlphabeticalSorting: { [key: string]: CustomSortSpec } = {
447+
"AAA": {
448+
defaultOrder: CustomSortOrder.trueAlphabetical,
449+
groups: [{
450+
order: CustomSortOrder.trueAlphabetical,
451+
type: CustomSortGroupType.Outsiders
452+
}],
453+
outsidersGroupIdx: 0,
454+
targetFoldersPaths: ['AAA']
455+
},
456+
"BBB": {
457+
defaultOrder: CustomSortOrder.trueAlphabeticalReverse,
458+
groups: [{
459+
order: CustomSortOrder.trueAlphabeticalReverse,
460+
type: CustomSortGroupType.Outsiders
461+
}],
462+
outsidersGroupIdx: 0,
463+
targetFoldersPaths: ['BBB']
464+
}
465+
}
466+
467+
describe('SortingSpecProcessor', () => {
468+
let processor: SortingSpecProcessor;
469+
beforeEach(() => {
470+
processor = new SortingSpecProcessor();
471+
});
472+
it('should recognize the true alphabetical (and reverse) sorting attribute for a folder', () => {
473+
const inputTxtArr: Array<string> = txtInputTrueAlphabeticalSortAttr.split('\n')
474+
const result = processor.parseSortSpecFromText(inputTxtArr, 'mock-folder', 'custom-name-note.md')
475+
expect(result?.sortSpecByPath).toEqual(expectedSortSpecForTrueAlphabeticalSorting)
476+
})
477+
})
478+
439479
const txtInputSimplistic1: string = `
440480
target-folder: /*
441481
/:files

src/custom-sort/sorting-spec-processor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ interface CustomSortOrderAscDescPair {
8484
// remember about .toLowerCase() before comparison!
8585
const OrderLiterals: { [key: string]: CustomSortOrderAscDescPair } = {
8686
'a-z': {asc: CustomSortOrder.alphabetical, desc: CustomSortOrder.alphabeticalReverse},
87+
'true a-z': {asc: CustomSortOrder.trueAlphabetical, desc: CustomSortOrder.trueAlphabeticalReverse},
8788
'created': {asc: CustomSortOrder.byCreatedTime, desc: CustomSortOrder.byCreatedTimeReverse},
8889
'modified': {asc: CustomSortOrder.byModifiedTime, desc: CustomSortOrder.byModifiedTimeReverse},
8990
'advanced modified': {asc: CustomSortOrder.byModifiedTimeAdvanced, desc: CustomSortOrder.byModifiedTimeReverseAdvanced},

0 commit comments

Comments
 (0)