Skip to content

Commit

Permalink
Merge pull request #288 from komarovalexander/dev
Browse files Browse the repository at this point in the history
Fix case sensitive string sorting
  • Loading branch information
komarovalexander authored Mar 6, 2023
2 parents a592cce + 88bf3dc commit 5245218
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ka-table",
"version": "7.11.0",
"version": "7.11.1",
"license": "MIT",
"repository": "github:komarovalexander/ka-table",
"homepage": "https://komarovalexander.github.io/ka-table/#/overview",
Expand Down
25 changes: 23 additions & 2 deletions src/lib/Utils/SortUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { DataType, SortDirection, SortingMode } from '../enums';
import { Column } from '../Models/Column';
import {
isMultipleSorting, isRemoteSorting, isSortingEnabled, isTripleStateSorting, sortColumns, sortData,
isMultipleSorting,
isRemoteSorting,
isSortingEnabled,
isTripleStateSorting,
sortColumns,
sortData,
} from './SortUtils';

import { Column } from '../Models/Column';

const data: any[] = [
{ column: 1, id: 1 },
{ column: null, id: 6 },
Expand All @@ -20,6 +26,21 @@ const columns: Column[] = [
];

describe('sortData', () => {
describe('strings', () => {
it('should be case insensitive', () => {
const stringData: any[] = [
{ column: 'A', id: 1 },
{ column: null, id: 6 },
{ column: 'a', id: 2 },
{ column: 'Bb', id: 3 },
{ column: 'cC', id: 4 },
{ column: 'aa', id: 5 },
{ column: null, id: 7 },
];
const newData = sortData(columns, stringData);
expect(newData).toMatchSnapshot();
});
});
it('should not change original data', () => {
const newData = sortData(columns, data);
expect(newData).not.toBe(data);
Expand Down
7 changes: 7 additions & 0 deletions src/lib/Utils/SortUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SortDirection, SortingMode } from '../enums';

import { Column } from '../Models/Column';
import { SortFunc } from '../types';
import { getValueByColumn } from './DataUtils';
Expand Down Expand Up @@ -47,6 +48,9 @@ const ascendSort = (sortedColumn: Column) => {
} else if (bValue == null) {
return 1;
}
if (typeof aValue === 'string' && typeof bValue === 'string'){
return aValue.toLowerCase() < bValue.toLowerCase() ? -1 : 1;
}
return aValue < bValue ? -1 : 1;
};
};
Expand All @@ -62,6 +66,9 @@ const descendSort = (sortedColumn: Column) => {
} else if (bValue == null) {
return -1;
}
if (typeof aValue === 'string' && typeof bValue === 'string'){
return aValue.toLowerCase() > bValue.toLowerCase() ? -1 : 1;
}
return aValue > bValue ? -1 : 1;
};
};
Expand Down
33 changes: 33 additions & 0 deletions src/lib/Utils/__snapshots__/SortUtils.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,36 @@ Array [
},
]
`;

exports[`sortData strings should be case insensitive 1`] = `
Array [
Object {
"column": "cC",
"id": 4,
},
Object {
"column": "Bb",
"id": 3,
},
Object {
"column": "aa",
"id": 5,
},
Object {
"column": "A",
"id": 1,
},
Object {
"column": "a",
"id": 2,
},
Object {
"column": null,
"id": 6,
},
Object {
"column": null,
"id": 7,
},
]
`;

0 comments on commit 5245218

Please sign in to comment.