From 593014170aec88bccd2464793db56981597e741d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Guilherme?= <105402150+guilhermeverissimo14@users.noreply.github.com> Date: Thu, 26 Mar 2026 21:22:00 -0300 Subject: [PATCH] =?UTF-8?q?fix(lookup):=20corrige=20ordena=C3=A7=C3=A3o=20?= =?UTF-8?q?de=20campos=20num=C3=A9ricos=20e=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A pesquisa em campos lookup com valores numéricos ou data não estava ordenando corretamente. Ao tentar ordenar por valores numéricos como "mass" no exemplo do portal, a ordenação ficava incorreta tanto ascendente quanto descendente. A função sortValues foi ajustada para detectar strings numéricas e convertê-las para números antes da comparação, garantindo ordenação numérica ao invés de alfabética. Foram adicionados testes para validar a conversão de strings numéricas, strings vazias, decimais e números negativos. Fixes #2674 --- projects/ui/src/lib/utils/util.spec.ts | 71 +++++++++++++++++++++++--- projects/ui/src/lib/utils/util.ts | 13 ++++- 2 files changed, 74 insertions(+), 10 deletions(-) diff --git a/projects/ui/src/lib/utils/util.spec.ts b/projects/ui/src/lib/utils/util.spec.ts index 781ddd3a8c..ee5adf04ec 100644 --- a/projects/ui/src/lib/utils/util.spec.ts +++ b/projects/ui/src/lib/utils/util.spec.ts @@ -846,22 +846,77 @@ describe('Function sortValues:', () => { expect(sortValues(leftSide, rightSide, ascending)).toBe(expectedReturn); }); - it('should return `-1` if `leftSide` is numeric value, `rightSide` is a character value and `ascending` is `true`', () => { + it('should convert numeric strings to numbers and sort correctly ascending', () => { const ascending: boolean = true; - const expectedReturn: number = -1; - const leftSide: string = '123'; - const rightSide: string = 'ABC'; + const leftSide: string = '10'; + const rightSide: string = '100'; - expect(sortValues(leftSide, rightSide, ascending)).toBe(expectedReturn); + expect(sortValues(leftSide, rightSide, ascending)).toBe(-1); }); - it('should return `1` if `leftSide` is numeric value, `rightSide` is a character value and `ascending` is `false`', () => { + it('should convert numeric strings to numbers and sort correctly descending', () => { const ascending: boolean = false; - const expectedReturn: number = 1; + const leftSide: string = '10'; + const rightSide: string = '100'; + + expect(sortValues(leftSide, rightSide, ascending)).toBe(1); + }); + + it('should handle numeric string comparison correctly (100 > 20 numerically)', () => { + const ascending: boolean = true; + const leftSide: string = '100'; + const rightSide: string = '20'; + + expect(sortValues(leftSide, rightSide, ascending)).toBe(1); + }); + + it('should not convert empty string to number', () => { + const ascending: boolean = true; + const leftSide: string = ''; + const rightSide: string = 'ABC'; + + expect(sortValues(leftSide, rightSide, ascending)).toBe(-1); + }); + + it('should not convert string with only spaces to number', () => { + const ascending: boolean = true; + const leftSide: string = ' '; + const rightSide: string = 'ABC'; + + expect(sortValues(leftSide, rightSide, ascending)).toBe(-1); + }); + + it('should handle decimal numeric strings', () => { + const ascending: boolean = true; + const leftSide: string = '10.5'; + const rightSide: string = '10.9'; + + expect(sortValues(leftSide, rightSide, ascending)).toBe(-1); + }); + + it('should handle negative numeric strings', () => { + const ascending: boolean = true; + const leftSide: string = '-10'; + const rightSide: string = '10'; + + expect(sortValues(leftSide, rightSide, ascending)).toBe(-1); + }); + + it('should compare equal numeric strings', () => { + const ascending: boolean = true; const leftSide: string = '123'; + const rightSide: string = '123'; + + expect(sortValues(leftSide, rightSide, ascending)).toBe(0); + }); + + it('should not convert alphanumeric string starting with number', () => { + const ascending: boolean = true; + const leftSide: string = '123abc'; const rightSide: string = 'ABC'; - expect(sortValues(leftSide, rightSide, ascending)).toBe(expectedReturn); + // '123abc' permanece string, então comparação alfabética + expect(sortValues(leftSide, rightSide, ascending)).toBe(-1); }); }); diff --git a/projects/ui/src/lib/utils/util.ts b/projects/ui/src/lib/utils/util.ts index dd31552893..48c6977311 100644 --- a/projects/ui/src/lib/utils/util.ts +++ b/projects/ui/src/lib/utils/util.ts @@ -419,8 +419,17 @@ export function getFormattedLink(link: string): string { * @param ascending Determina se será em ordem ascendente ou descendente. */ export function sortValues(leftSide: string | Date, rightSide: string | Date, ascending: boolean = true): number { - const left = isTypeof(leftSide, 'string') ? (leftSide as string).toLowerCase() : leftSide; - const right = isTypeof(rightSide, 'string') ? (rightSide as string).toLowerCase() : rightSide; + + const leftNumeric = isTypeof(leftSide, 'string') && !isNaN(Number(leftSide)) && leftSide.toString().trim() !== '' + ? Number(leftSide) + : leftSide; + + const rightNumeric = isTypeof(rightSide, 'string') && !isNaN(Number(rightSide)) && rightSide.toString().trim() !== '' + ? Number(rightSide) + : rightSide; + + const left = isTypeof(leftNumeric, 'string') ? (leftNumeric as string).toLowerCase() : leftNumeric; + const right = isTypeof(rightNumeric, 'string') ? (rightNumeric as string).toLowerCase() : rightNumeric; const leftIsInvalid = left === null || left === undefined || Number.isNaN(left); const rightIsInvalid = right === null || right === undefined || Number.isNaN(right);