diff --git a/src/custom-sort/custom-sort.spec.ts b/src/custom-sort/custom-sort.spec.ts index 5a2bc6b..db974c3 100644 --- a/src/custom-sort/custom-sort.spec.ts +++ b/src/custom-sort/custom-sort.spec.ts @@ -2276,7 +2276,7 @@ describe('CustomSortOrder.byMetadataFieldAlphabetical', () => { expect(result1).toBe(SORT_FIRST_GOES_EARLIER) expect(result2).toBe(SORT_FIRST_GOES_LATER) }) - it('should correctly fallback to alphabetical by name when metadata on both items is present and equal', () => { + it('should correctly compare when metadata on both items is present and equal', () => { // given const itemA: Partial = { metadataFieldValue: 'Aaa', @@ -2294,9 +2294,9 @@ describe('CustomSortOrder.byMetadataFieldAlphabetical', () => { const result3: number = sorter(itemB as FolderItemForSorting, itemB as FolderItemForSorting) // then - expect(result1).toBe(SORT_FIRST_GOES_LATER) - expect(result2).toBe(SORT_FIRST_GOES_EARLIER) - expect(result3).toBe(SORT_ITEMS_ARE_EQUAL) + expect(result1).toBe(EQUAL_OR_UNCOMPARABLE) + expect(result2).toBe(EQUAL_OR_UNCOMPARABLE) + expect(result3).toBe(EQUAL_OR_UNCOMPARABLE) }) it('should put the item with metadata earlier if the second one has no metadata ', () => { // given @@ -2358,7 +2358,7 @@ describe('CustomSortOrder.byMetadataFieldAlphabeticalReverse', () => { expect(result1).toBe(SORT_FIRST_GOES_LATER) expect(result2).toBe(SORT_FIRST_GOES_EARLIER) }) - it('should correctly fallback to alphabetical reverse by name when metadata on both items is present and equal', () => { + it('should correctly compare when metadata on both items is present and equal', () => { // given const itemA: Partial = { metadataFieldValue: 'Aaa', @@ -2376,9 +2376,9 @@ describe('CustomSortOrder.byMetadataFieldAlphabeticalReverse', () => { const result3: number = sorter(itemB as FolderItemForSorting, itemB as FolderItemForSorting) // then - expect(result1).toBe(SORT_FIRST_GOES_EARLIER) - expect(result2).toBe(SORT_FIRST_GOES_LATER) - expect(result3).toBe(SORT_ITEMS_ARE_EQUAL) + expect(result1).toBe(EQUAL_OR_UNCOMPARABLE) + expect(result2).toBe(EQUAL_OR_UNCOMPARABLE) + expect(result3).toBe(EQUAL_OR_UNCOMPARABLE) }) it('should put the item with metadata later if the second one has no metadata (reverse order)', () => { // given @@ -2425,9 +2425,9 @@ describe('sorterByMetadataField', () => { it.each([ [true,'abc','def',-1, 'a', 'a'], [true,'xyz','klm',1, 'b', 'b'], - [true,'mmm','mmm',0, 'c', 'c'], - [true,'mmm','mmm',-1, 'd', 'e'], - [true,'mmm','mmm',1, 'e', 'd'], + [true,'mmm','mmm',EQUAL_OR_UNCOMPARABLE, 'c', 'c'], + [true,'mmm','mmm',EQUAL_OR_UNCOMPARABLE, 'd', 'e'], + [true,'mmm','mmm',EQUAL_OR_UNCOMPARABLE, 'e', 'd'], [true,'abc',undefined,-1, 'a','a'], [true,undefined,'klm',1, 'b','b'], [true,undefined,undefined,EQUAL_OR_UNCOMPARABLE, 'a','a'], @@ -2435,9 +2435,9 @@ describe('sorterByMetadataField', () => { [true,undefined,undefined,EQUAL_OR_UNCOMPARABLE, 'd','c'], [false,'abc','def',1, 'a', 'a'], [false,'xyz','klm',-1, 'b', 'b'], - [false,'mmm','mmm',0, 'c', 'c'], - [false,'mmm','mmm',1, 'd', 'e'], - [false,'mmm','mmm',-1, 'e', 'd'], + [false,'mmm','mmm',EQUAL_OR_UNCOMPARABLE, 'c', 'c'], + [false,'mmm','mmm',EQUAL_OR_UNCOMPARABLE, 'd', 'e'], + [false,'mmm','mmm',EQUAL_OR_UNCOMPARABLE, 'e', 'd'], [false,'abc',undefined,1, 'a','a'], [false,undefined,'klm',-1, 'b','b'], [false,undefined,undefined,EQUAL_OR_UNCOMPARABLE, 'a','a'], diff --git a/src/custom-sort/custom-sort.ts b/src/custom-sort/custom-sort.ts index 8ec2429..074cc9d 100644 --- a/src/custom-sort/custom-sort.ts +++ b/src/custom-sort/custom-sort.ts @@ -5,7 +5,8 @@ import { requireApiVersion, TAbstractFile, TFile, - TFolder} from 'obsidian'; + TFolder +} from 'obsidian'; import { determineStarredStatusOf, Starred_PluginInstance @@ -86,12 +87,7 @@ export const sorterByMetadataField:(reverseOrder?: boolean, trueAlphabetical?: b } if (a.metadataFieldValue && b.metadataFieldValue) { const sortResult: number = collatorCompareFn(a.metadataFieldValue, b.metadataFieldValue) - if (sortResult === 0) { - // Fallback -> requested sort by metadata and both items have the same metadata value - return collatorCompareFn(a.sortString, b.sortString) // switch to alphabetical sort by note/folder titles - } else { - return sortResult - } + return sortResult } // Item with metadata goes before the w/o metadata if (a.metadataFieldValue) return -1