Skip to content

fix(table): MatTableDataSource incorrectly sorting zero #10561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/lib/table/table-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,16 @@ export class MatTableDataSource<T> extends DataSource<T> {
// This avoids inconsistent results when comparing values to undefined/null.
// If neither value exists, return 0 (equal).
let comparatorResult = 0;
if (valueA && valueB) {
if (valueA != null && valueB != null) {
// Check if one value is greater than the other; if equal, comparatorResult should remain 0.
if (valueA > valueB) {
comparatorResult = 1;
} else if (valueA < valueB) {
comparatorResult = -1;
}
} else if (valueA) {
} else if (valueA != null) {
comparatorResult = 1;
} else if (valueB) {
} else if (valueB != null) {
comparatorResult = -1;
}

Expand Down
35 changes: 32 additions & 3 deletions src/lib/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,35 @@ describe('MatTable', () => {
]);
});

it('should sort zero correctly', () => {
// Activate column A sort
dataSource.data[0].a = 1;
dataSource.data[1].a = 0;
dataSource.data[2].a = -1;

// Expect that zero comes after the negative numbers and before the positive ones.
component.sort.sort(component.sortHeader);
fixture.detectChanges();
expectTableToMatchContent(tableElement, [
['Column A\xa0Sorted by a ascending', 'Column B', 'Column C'],
['-1', 'b_3', 'c_3'],
['0', 'b_2', 'c_2'],
['1', 'b_1', 'c_1'],
]);


// Expect that zero comes after the negative numbers and before
// the positive ones when switching the sorting direction.
component.sort.sort(component.sortHeader);
fixture.detectChanges();
expectTableToMatchContent(tableElement, [
['Column A\xa0Sorted by a descending', 'Column B', 'Column C'],
['1', 'b_1', 'c_1'],
['0', 'b_2', 'c_2'],
['-1', 'b_3', 'c_3'],
]);
});

it('should be able to page the table contents', fakeAsync(() => {
// Add 100 rows, should only display first 5 since page length is 5
for (let i = 0; i < 100; i++) {
Expand Down Expand Up @@ -279,9 +308,9 @@ describe('MatTable', () => {
});

interface TestData {
a: string|undefined;
b: string|undefined;
c: string|undefined;
a: string|number|undefined;
b: string|number|undefined;
c: string|number|undefined;
}

class FakeDataSource extends DataSource<TestData> {
Expand Down