[AAE-6153] Enable correct sorting while data are numbers (#7859)

* [AAE-6153] Enable correct sorting while data are numbers

* [AAE-6153] Add unit tests for bugfix

* Trigger travis
This commit is contained in:
Marcin Krueger 2022-09-27 14:29:13 +02:00 committed by GitHub
parent caec4a208d
commit a3447836b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 14 deletions

View File

@ -231,6 +231,21 @@ describe('ObjectDataTableAdapter', () => {
expect(rows[1].getValue('id')).toBe(1); expect(rows[1].getValue('id')).toBe(1);
}); });
it('should sort by numbers', () => {
const adapter = new ObjectDataTableAdapter([
{ id: 123 },
{ id: 38 },
{ id: 50 }
],[{key: 'id'} as DataColumn]);
adapter.setSorting(new DataSorting('id', 'asc'));
const rowsAsc = adapter.getRows();
expect(rowsAsc[0].getValue('id')).toBe(38);
expect(rowsAsc[1].getValue('id')).toBe(50);
expect(rowsAsc[2].getValue('id')).toBe(123);
});
it('should be sorting undefined if no sortable found', () => { it('should be sorting undefined if no sortable found', () => {
const adapter = new ObjectDataTableAdapter( const adapter = new ObjectDataTableAdapter(
[ [

View File

@ -131,13 +131,17 @@ export class ObjectDataTableAdapter implements DataTableAdapter {
if (sorting && sorting.key) { if (sorting && sorting.key) {
this._rows.sort((a: DataRow, b: DataRow) => { this._rows.sort((a: DataRow, b: DataRow) => {
let left = a.getValue(sorting.key); let left = a.getValue(sorting.key);
let right = b.getValue(sorting.key);
if (typeof left === 'number' && typeof right === 'number') {
return sorting.direction === 'asc' ? left - right : right - left;
} else {
if (left) { if (left) {
left = (left instanceof Date) ? left.valueOf().toString() : left.toString(); left = (left instanceof Date) ? left.valueOf().toString() : left.toString();
} else { } else {
left = ''; left = '';
} }
let right = b.getValue(sorting.key);
if (right) { if (right) {
right = (right instanceof Date) ? right.valueOf().toString() : right.toString(); right = (right instanceof Date) ? right.valueOf().toString() : right.toString();
} else { } else {
@ -147,6 +151,7 @@ export class ObjectDataTableAdapter implements DataTableAdapter {
return sorting.direction === 'asc' return sorting.direction === 'asc'
? left.localeCompare(right) ? left.localeCompare(right)
: right.localeCompare(left); : right.localeCompare(left);
}
}); });
} }
} }