From 02a7884e9adeb428d694e94683e83bf6c1c89d72 Mon Sep 17 00:00:00 2001 From: Kevin Van Cott Date: Fri, 13 Oct 2023 08:55:26 -0500 Subject: [PATCH] fix: revert better alphanumeric sort of bools and floats (fix #5108) (#5109) (#5129) This reverts commit 3f9d38231fca917cf36b64980a88f5bf40b434ca. --- packages/table-core/__tests__/Sorting.test.ts | 80 ------------------- packages/table-core/src/sortingFns.ts | 10 --- 2 files changed, 90 deletions(-) delete mode 100644 packages/table-core/__tests__/Sorting.test.ts diff --git a/packages/table-core/__tests__/Sorting.test.ts b/packages/table-core/__tests__/Sorting.test.ts deleted file mode 100644 index c7898d6601..0000000000 --- a/packages/table-core/__tests__/Sorting.test.ts +++ /dev/null @@ -1,80 +0,0 @@ -import { - ColumnDef, - createTable, - getCoreRowModel, - getSortedRowModel, -} from '../src' - -describe('Sorting', () => { - describe('alphanumeric sort booleans ascending', () => { - it('should return rows in correct ascending order', () => { - const data = [ - { value: false }, - { value: true }, - { value: false }, - { value: true }, - ] - const columns: ColumnDef<{value: boolean}>[] = [{ - accessorKey: "value", - header: "Value", - sortingFn: "alphanumeric" - }] - - const table = createTable({ - onStateChange() {}, - renderFallbackValue: '', - data, - state: { sorting: [{ - id: "value", - desc: false, - }]}, - columns, - getCoreRowModel: getCoreRowModel(), - getSortedRowModel: getSortedRowModel(), - }) - - const rowModel = table.getSortedRowModel() - - expect(rowModel.rows[0].getValue("value")).toBe(false) - expect(rowModel.rows[1].getValue("value")).toBe(false) - expect(rowModel.rows[2].getValue("value")).toBe(true) - expect(rowModel.rows[3].getValue("value")).toBe(true) - }) - }) - - describe('alphanumeric sort floats ascending', () => { - it('should return rows in correct ascending order', () => { - const data = [ - { value: 0.85 }, - { value: 0.001000000047 }, - { value: 0.2000016 }, - { value: 0.002002 }, - ] - const columns: ColumnDef<{value: number}>[] = [{ - accessorKey: "value", - header: "Value", - sortingFn: "alphanumeric" - }] - - const table = createTable({ - onStateChange() {}, - renderFallbackValue: '', - data, - state: { sorting: [{ - id: "value", - desc: false, - }]}, - columns, - getCoreRowModel: getCoreRowModel(), - getSortedRowModel: getSortedRowModel(), - }) - - const rowModel = table.getSortedRowModel() - - expect(rowModel.rows[0].getValue("value")).toBe(0.001000000047) - expect(rowModel.rows[1].getValue("value")).toBe(0.002002) - expect(rowModel.rows[2].getValue("value")).toBe(0.2000016) - expect(rowModel.rows[3].getValue("value")).toBe(0.85) - }) - }) -}) diff --git a/packages/table-core/src/sortingFns.ts b/packages/table-core/src/sortingFns.ts index 287a6e36ed..5cb88dac5f 100644 --- a/packages/table-core/src/sortingFns.ts +++ b/packages/table-core/src/sortingFns.ts @@ -55,9 +55,6 @@ function compareBasic(a: any, b: any) { } function toString(a: any) { - if (typeof a === 'boolean') { - return String(a) - } if (typeof a === 'number') { if (isNaN(a) || a === Infinity || a === -Infinity) { return '' @@ -74,13 +71,6 @@ function toString(a: any) { // It handles numbers, mixed alphanumeric combinations, and even // null, undefined, and Infinity function compareAlphanumeric(aStr: string, bStr: string) { - // Check if the string contains only a number - const aFloat = parseFloat(aStr); - const bFloat = parseFloat(bStr); - if(!isNaN(aFloat) && !isNaN(bFloat)) { - return compareBasic(aFloat, bFloat) - } - // Split on number groups, but keep the delimiter // Then remove falsey split values const a = aStr.split(reSplitAlphaNumeric).filter(Boolean)