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);
|
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(
|
||||||
[
|
[
|
||||||
|
@ -131,22 +131,27 @@ 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);
|
||||||
if (left) {
|
|
||||||
left = (left instanceof Date) ? left.valueOf().toString() : left.toString();
|
|
||||||
} else {
|
|
||||||
left = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
let right = b.getValue(sorting.key);
|
let right = b.getValue(sorting.key);
|
||||||
if (right) {
|
|
||||||
right = (right instanceof Date) ? right.valueOf().toString() : right.toString();
|
|
||||||
} else {
|
|
||||||
right = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
return sorting.direction === 'asc'
|
if (typeof left === 'number' && typeof right === 'number') {
|
||||||
? left.localeCompare(right)
|
return sorting.direction === 'asc' ? left - right : right - left;
|
||||||
: right.localeCompare(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);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user