diff --git a/test/compare.test.ts b/test/compare.test.ts index e2c9445fa..f34d74b54 100644 --- a/test/compare.test.ts +++ b/test/compare.test.ts @@ -249,6 +249,21 @@ describe('compare', () => { }), ).toBe(0) }) + + it('gives maximum priority to void', () => { + expect( + compare(createTestNode({ name: 'a' }), createTestNode({ name: '' }), { + ...compareOptions, + alphabet: 'a', + }), + ).toBe(1) + expect( + compare(createTestNode({ name: '' }), createTestNode({ name: 'a' }), { + ...compareOptions, + alphabet: 'a', + }), + ).toBe(-1) + }) }) let createTestNode = ({ name }: { name: string }): SortingNode => diff --git a/utils/compare.ts b/utils/compare.ts index 13aba43c1..ed43d9e2a 100644 --- a/utils/compare.ts +++ b/utils/compare.ts @@ -122,9 +122,9 @@ let getCustomSortingFunction = ( return (aNode: T, bNode: T) => { let aValue = formatString(nodeValueGetter(aNode)) let bValue = formatString(nodeValueGetter(bNode)) + let minLength = Math.min(aValue.length, bValue.length) // Iterate character by character - // eslint-disable-next-line unicorn/no-for-loop - for (let i = 0; i < aValue.length; i++) { + for (let i = 0; i < minLength; i++) { let aCharacter = aValue[i] let bCharacter = bValue[i] let indexOfA = indexByCharacters.get(aCharacter) @@ -135,7 +135,10 @@ let getCustomSortingFunction = ( return convertBooleanToSign(indexOfA - indexOfB > 0) } } - return 0 + if (aValue.length === bValue.length) { + return 0 + } + return convertBooleanToSign(aValue.length - bValue.length > 0) } }