mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-12 17:04:57 +00:00
[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:
parent
caec4a208d
commit
a3447836b1
@ -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(
|
||||
[
|
||||
|
@ -131,13 +131,17 @@ export class ObjectDataTableAdapter implements DataTableAdapter {
|
||||
if (sorting && sorting.key) {
|
||||
this._rows.sort((a: DataRow, b: DataRow) => {
|
||||
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) {
|
||||
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 {
|
||||
@ -147,6 +151,7 @@ export class ObjectDataTableAdapter implements DataTableAdapter {
|
||||
return sorting.direction === 'asc'
|
||||
? left.localeCompare(right)
|
||||
: right.localeCompare(left);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user