diff --git a/lib/core/src/lib/datatable/data/object-datatable-adapter.spec.ts b/lib/core/src/lib/datatable/data/object-datatable-adapter.spec.ts index 6bdc010f26..58b4506507 100644 --- a/lib/core/src/lib/datatable/data/object-datatable-adapter.spec.ts +++ b/lib/core/src/lib/datatable/data/object-datatable-adapter.spec.ts @@ -231,6 +231,21 @@ describe('ObjectDataTableAdapter', () => { 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', () => { const adapter = new ObjectDataTableAdapter( [ diff --git a/lib/core/src/lib/datatable/data/object-datatable-adapter.ts b/lib/core/src/lib/datatable/data/object-datatable-adapter.ts index 665946d8ae..3cbdc5cae4 100644 --- a/lib/core/src/lib/datatable/data/object-datatable-adapter.ts +++ b/lib/core/src/lib/datatable/data/object-datatable-adapter.ts @@ -131,22 +131,27 @@ export class ObjectDataTableAdapter implements DataTableAdapter { if (sorting && sorting.key) { this._rows.sort((a: DataRow, b: DataRow) => { let left = a.getValue(sorting.key); - if (left) { - left = (left instanceof Date) ? left.valueOf().toString() : left.toString(); - } else { - left = ''; - } - let right = b.getValue(sorting.key); - if (right) { - right = (right instanceof Date) ? right.valueOf().toString() : right.toString(); - } else { - right = ''; - } - return sorting.direction === 'asc' - ? left.localeCompare(right) - : right.localeCompare(left); + if (typeof left === 'number' && typeof right === 'number') { + return sorting.direction === 'asc' ? left - right : right - left; + } else { + if (left) { + left = (left instanceof Date) ? left.valueOf().toString() : left.toString(); + } else { + left = ''; + } + + if (right) { + right = (right instanceof Date) ? right.valueOf().toString() : right.toString(); + } else { + right = ''; + } + + return sorting.direction === 'asc' + ? left.localeCompare(right) + : right.localeCompare(left); + } }); } }