#89 - Support for multi-level sorting

- fix in comparator by metadata to support multi-level sorting
- update of relevant existing unit tests
This commit is contained in:
SebastianMC 2023-08-29 00:17:58 +02:00
parent f72c4d61da
commit 76ee3dfa62
2 changed files with 15 additions and 20 deletions

View File

@ -2275,7 +2275,7 @@ describe('CustomSortOrder.byMetadataFieldAlphabetical', () => {
expect(result1).toBe(SORT_FIRST_GOES_EARLIER) expect(result1).toBe(SORT_FIRST_GOES_EARLIER)
expect(result2).toBe(SORT_FIRST_GOES_LATER) 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 // given
const itemA: Partial<FolderItemForSorting> = { const itemA: Partial<FolderItemForSorting> = {
metadataFieldValue: 'Aaa', metadataFieldValue: 'Aaa',
@ -2293,9 +2293,9 @@ describe('CustomSortOrder.byMetadataFieldAlphabetical', () => {
const result3: number = sorter(itemB as FolderItemForSorting, itemB as FolderItemForSorting) const result3: number = sorter(itemB as FolderItemForSorting, itemB as FolderItemForSorting)
// then // then
expect(result1).toBe(SORT_FIRST_GOES_LATER) expect(result1).toBe(EQUAL_OR_UNCOMPARABLE)
expect(result2).toBe(SORT_FIRST_GOES_EARLIER) expect(result2).toBe(EQUAL_OR_UNCOMPARABLE)
expect(result3).toBe(SORT_ITEMS_ARE_EQUAL) expect(result3).toBe(EQUAL_OR_UNCOMPARABLE)
}) })
it('should put the item with metadata earlier if the second one has no metadata ', () => { it('should put the item with metadata earlier if the second one has no metadata ', () => {
// given // given
@ -2357,7 +2357,7 @@ describe('CustomSortOrder.byMetadataFieldAlphabeticalReverse', () => {
expect(result1).toBe(SORT_FIRST_GOES_LATER) expect(result1).toBe(SORT_FIRST_GOES_LATER)
expect(result2).toBe(SORT_FIRST_GOES_EARLIER) 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 // given
const itemA: Partial<FolderItemForSorting> = { const itemA: Partial<FolderItemForSorting> = {
metadataFieldValue: 'Aaa', metadataFieldValue: 'Aaa',
@ -2375,9 +2375,9 @@ describe('CustomSortOrder.byMetadataFieldAlphabeticalReverse', () => {
const result3: number = sorter(itemB as FolderItemForSorting, itemB as FolderItemForSorting) const result3: number = sorter(itemB as FolderItemForSorting, itemB as FolderItemForSorting)
// then // then
expect(result1).toBe(SORT_FIRST_GOES_EARLIER) expect(result1).toBe(EQUAL_OR_UNCOMPARABLE)
expect(result2).toBe(SORT_FIRST_GOES_LATER) expect(result2).toBe(EQUAL_OR_UNCOMPARABLE)
expect(result3).toBe(SORT_ITEMS_ARE_EQUAL) expect(result3).toBe(EQUAL_OR_UNCOMPARABLE)
}) })
it('should put the item with metadata later if the second one has no metadata (reverse order)', () => { it('should put the item with metadata later if the second one has no metadata (reverse order)', () => {
// given // given
@ -2424,9 +2424,9 @@ describe('sorterByMetadataField', () => {
it.each([ it.each([
[true,'abc','def',-1, 'a', 'a'], [true,'abc','def',-1, 'a', 'a'],
[true,'xyz','klm',1, 'b', 'b'], [true,'xyz','klm',1, 'b', 'b'],
[true,'mmm','mmm',0, 'c', 'c'], [true,'mmm','mmm',EQUAL_OR_UNCOMPARABLE, 'c', 'c'],
[true,'mmm','mmm',-1, 'd', 'e'], [true,'mmm','mmm',EQUAL_OR_UNCOMPARABLE, 'd', 'e'],
[true,'mmm','mmm',1, 'e', 'd'], [true,'mmm','mmm',EQUAL_OR_UNCOMPARABLE, 'e', 'd'],
[true,'abc',undefined,-1, 'a','a'], [true,'abc',undefined,-1, 'a','a'],
[true,undefined,'klm',1, 'b','b'], [true,undefined,'klm',1, 'b','b'],
[true,undefined,undefined,EQUAL_OR_UNCOMPARABLE, 'a','a'], [true,undefined,undefined,EQUAL_OR_UNCOMPARABLE, 'a','a'],
@ -2434,9 +2434,9 @@ describe('sorterByMetadataField', () => {
[true,undefined,undefined,EQUAL_OR_UNCOMPARABLE, 'd','c'], [true,undefined,undefined,EQUAL_OR_UNCOMPARABLE, 'd','c'],
[false,'abc','def',1, 'a', 'a'], [false,'abc','def',1, 'a', 'a'],
[false,'xyz','klm',-1, 'b', 'b'], [false,'xyz','klm',-1, 'b', 'b'],
[false,'mmm','mmm',0, 'c', 'c'], [false,'mmm','mmm',EQUAL_OR_UNCOMPARABLE, 'c', 'c'],
[false,'mmm','mmm',1, 'd', 'e'], [false,'mmm','mmm',EQUAL_OR_UNCOMPARABLE, 'd', 'e'],
[false,'mmm','mmm',-1, 'e', 'd'], [false,'mmm','mmm',EQUAL_OR_UNCOMPARABLE, 'e', 'd'],
[false,'abc',undefined,1, 'a','a'], [false,'abc',undefined,1, 'a','a'],
[false,undefined,'klm',-1, 'b','b'], [false,undefined,'klm',-1, 'b','b'],
[false,undefined,undefined,EQUAL_OR_UNCOMPARABLE, 'a','a'], [false,undefined,undefined,EQUAL_OR_UNCOMPARABLE, 'a','a'],

View File

@ -83,12 +83,7 @@ export const sorterByMetadataField:(reverseOrder?: boolean, trueAlphabetical?: b
} }
if (a.metadataFieldValue && b.metadataFieldValue) { if (a.metadataFieldValue && b.metadataFieldValue) {
const sortResult: number = collatorCompareFn(a.metadataFieldValue, b.metadataFieldValue) const sortResult: number = collatorCompareFn(a.metadataFieldValue, b.metadataFieldValue)
if (sortResult === 0) { return sortResult
// 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
}
} }
// Item with metadata goes before the w/o metadata // Item with metadata goes before the w/o metadata
if (a.metadataFieldValue) return -1 if (a.metadataFieldValue) return -1