Skip to content

Commit bcb5697

Browse files
crisbetojelbourn
authored andcommitted
fix(table): MatTableDataSource incorrectly sorting zero (#10561)
Fixes the `MatTableDataSource` sorting zero before negative numbers. Fixes #10556.
1 parent 6575d9c commit bcb5697

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/lib/table/table-data-source.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,16 @@ export class MatTableDataSource<T> extends DataSource<T> {
123123
// This avoids inconsistent results when comparing values to undefined/null.
124124
// If neither value exists, return 0 (equal).
125125
let comparatorResult = 0;
126-
if (valueA && valueB) {
126+
if (valueA != null && valueB != null) {
127127
// Check if one value is greater than the other; if equal, comparatorResult should remain 0.
128128
if (valueA > valueB) {
129129
comparatorResult = 1;
130130
} else if (valueA < valueB) {
131131
comparatorResult = -1;
132132
}
133-
} else if (valueA) {
133+
} else if (valueA != null) {
134134
comparatorResult = 1;
135-
} else if (valueB) {
135+
} else if (valueB != null) {
136136
comparatorResult = -1;
137137
}
138138

src/lib/table/table.spec.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,35 @@ describe('MatTable', () => {
267267
]);
268268
});
269269

270+
it('should sort zero correctly', () => {
271+
// Activate column A sort
272+
dataSource.data[0].a = 1;
273+
dataSource.data[1].a = 0;
274+
dataSource.data[2].a = -1;
275+
276+
// Expect that zero comes after the negative numbers and before the positive ones.
277+
component.sort.sort(component.sortHeader);
278+
fixture.detectChanges();
279+
expectTableToMatchContent(tableElement, [
280+
['Column A\xa0Sorted by a ascending', 'Column B', 'Column C'],
281+
['-1', 'b_3', 'c_3'],
282+
['0', 'b_2', 'c_2'],
283+
['1', 'b_1', 'c_1'],
284+
]);
285+
286+
287+
// Expect that zero comes after the negative numbers and before
288+
// the positive ones when switching the sorting direction.
289+
component.sort.sort(component.sortHeader);
290+
fixture.detectChanges();
291+
expectTableToMatchContent(tableElement, [
292+
['Column A\xa0Sorted by a descending', 'Column B', 'Column C'],
293+
['1', 'b_1', 'c_1'],
294+
['0', 'b_2', 'c_2'],
295+
['-1', 'b_3', 'c_3'],
296+
]);
297+
});
298+
270299
it('should be able to page the table contents', fakeAsync(() => {
271300
// Add 100 rows, should only display first 5 since page length is 5
272301
for (let i = 0; i < 100; i++) {
@@ -299,9 +328,9 @@ describe('MatTable', () => {
299328
});
300329

301330
interface TestData {
302-
a: string|undefined;
303-
b: string|undefined;
304-
c: string|undefined;
331+
a: string|number|undefined;
332+
b: string|number|undefined;
333+
c: string|number|undefined;
305334
}
306335

307336
class FakeDataSource extends DataSource<TestData> {

0 commit comments

Comments
 (0)