From c8ec181f743d16ba7288d1f3f864d6865fc05ef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Est=C3=A9vez=20Rocha?= Date: Fri, 31 Jul 2020 18:26:45 +0200 Subject: [PATCH 1/9] fix(material/table): Sort correctly #20140 Sort correctly when column information has string and numeric values Fixes #20140 --- src/material/table/table-data-source.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/material/table/table-data-source.ts b/src/material/table/table-data-source.ts index a6e80a39b09c..0ddbb8f72a6b 100644 --- a/src/material/table/table-data-source.ts +++ b/src/material/table/table-data-source.ts @@ -148,6 +148,14 @@ export class MatTableDataSource extends DataSource { let valueA = this.sortingDataAccessor(a, active); let valueB = this.sortingDataAccessor(b, active); + const valueAType = typeof valueA; + const valueBType = typeof valueB; + const sameType = valueAType === valueBType; + if (!sameType) { + valueA = valueAType === 'number' ? valueA.toString() : valueA; + valueB = valueBType === 'number' ? valueB.toString() : valueB; + } + // If both valueA and valueB exist (truthy), then compare the two. Otherwise, check if // one value exists while the other doesn't. In this case, existing value should come last. // This avoids inconsistent results when comparing values to undefined/null. From b75527355f91d62d9d8f1a774017e068b5fb504a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Est=C3=A9vez=20Rocha?= Date: Fri, 31 Jul 2020 18:26:48 +0200 Subject: [PATCH 2/9] fix(material/table): Sort correctly #20140 Sort correctly when column information has string and numeric values Fixes #20140 --- src/material/table/table-data-source.spec.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/material/table/table-data-source.spec.ts b/src/material/table/table-data-source.spec.ts index 98a1de02315b..67e80f78e7ea 100644 --- a/src/material/table/table-data-source.spec.ts +++ b/src/material/table/table-data-source.spec.ts @@ -47,6 +47,10 @@ describe('MatTableDataSource', () => { it('should be able to correctly sort an array of string', () => { testSortWithValues(['apples', 'bananas', 'cherries', 'lemons', 'strawberries']); }); + + it('should be able to correctly sort an array of strings and numbers', () => { + testSortWithValues([3, 'one', 'two', 'four', 'five']); + }); }); }); From 38c714d942c99134f4ec441177bf828583228e65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Est=C3=A9vez=20Rocha?= Date: Fri, 31 Jul 2020 18:42:43 +0200 Subject: [PATCH 3/9] fix(material/table): Sort correctly #20140 Sort correctly when column information has string and numeric values --- src/material/table/table-data-source.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/material/table/table-data-source.spec.ts b/src/material/table/table-data-source.spec.ts index 67e80f78e7ea..96596f106c27 100644 --- a/src/material/table/table-data-source.spec.ts +++ b/src/material/table/table-data-source.spec.ts @@ -47,9 +47,9 @@ describe('MatTableDataSource', () => { it('should be able to correctly sort an array of string', () => { testSortWithValues(['apples', 'bananas', 'cherries', 'lemons', 'strawberries']); }); - + it('should be able to correctly sort an array of strings and numbers', () => { - testSortWithValues([3, 'one', 'two', 'four', 'five']); + testSortWithValues(['apples', 'bananas', 'cherries', 'lemons', 'strawberries', 3]); }); }); }); From 93301b1b04b89f9b4c8840d49eae082cf7b0eff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Est=C3=A9vez=20Rocha?= Date: Fri, 31 Jul 2020 18:48:24 +0200 Subject: [PATCH 4/9] fix(material/table): Sort correctly Sort correctly when column information has string and numeric values --- src/material/table/table-data-source.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/material/table/table-data-source.spec.ts b/src/material/table/table-data-source.spec.ts index 96596f106c27..0304e72594c0 100644 --- a/src/material/table/table-data-source.spec.ts +++ b/src/material/table/table-data-source.spec.ts @@ -49,7 +49,7 @@ describe('MatTableDataSource', () => { }); it('should be able to correctly sort an array of strings and numbers', () => { - testSortWithValues(['apples', 'bananas', 'cherries', 'lemons', 'strawberries', 3]); + testSortWithValues([3, 'apples', 'bananas', 'cherries', 'lemons', 'strawberries']); }); }); }); From eb6853b47cb85b07d1054ffe47e0c30fae265459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Est=C3=A9vez=20Rocha?= Date: Wed, 5 Aug 2020 16:30:13 +0200 Subject: [PATCH 5/9] fix(material/table): Sort correctly Correctly sort the data in a column when it contains string data and numeric values. If this occurs, numeric values are converted to text --- src/material/table/table-data-source.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/material/table/table-data-source.ts b/src/material/table/table-data-source.ts index 0ddbb8f72a6b..210db6e9fd43 100644 --- a/src/material/table/table-data-source.ts +++ b/src/material/table/table-data-source.ts @@ -148,12 +148,18 @@ export class MatTableDataSource extends DataSource { let valueA = this.sortingDataAccessor(a, active); let valueB = this.sortingDataAccessor(b, active); + // If there are data in the column that can be converted to a number, + // it must be ensured that the rest of the data is of the same type so as not to order incorrectly. const valueAType = typeof valueA; const valueBType = typeof valueB; - const sameType = valueAType === valueBType; - if (!sameType) { - valueA = valueAType === 'number' ? valueA.toString() : valueA; - valueB = valueBType === 'number' ? valueB.toString() : valueB; + + if valueAType !== valueBType) { + if (valueAType === 'number'){ + valueA += ''; + } + if (valueBType === 'number'){ + valueB += ''; + } } // If both valueA and valueB exist (truthy), then compare the two. Otherwise, check if From c56da2e2421cbf114ad31eb9832bf8734743fbd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Est=C3=A9vez=20Rocha?= Date: Wed, 5 Aug 2020 16:31:48 +0200 Subject: [PATCH 6/9] fix(material/table): Sort correctly Correctly sort the data in a column when it contains string data and numeric values. If this occurs, numeric values are converted to text --- src/material/table/table-data-source.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/material/table/table-data-source.ts b/src/material/table/table-data-source.ts index 210db6e9fd43..134960d0231d 100644 --- a/src/material/table/table-data-source.ts +++ b/src/material/table/table-data-source.ts @@ -153,7 +153,7 @@ export class MatTableDataSource extends DataSource { const valueAType = typeof valueA; const valueBType = typeof valueB; - if valueAType !== valueBType) { + if (valueAType !== valueBType) { if (valueAType === 'number'){ valueA += ''; } From bff4daf3c75bbce7f4d59a75dc8253b7d1fb6d26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Est=C3=A9vez=20Rocha?= Date: Wed, 5 Aug 2020 16:39:31 +0200 Subject: [PATCH 7/9] fix(material/table): Sort correctly Correctly sort the data in a column when it contains string data and numeric values. If this occurs, numeric values are converted to text --- src/material/table/table-data-source.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/material/table/table-data-source.ts b/src/material/table/table-data-source.ts index 134960d0231d..74c2b132ef3b 100644 --- a/src/material/table/table-data-source.ts +++ b/src/material/table/table-data-source.ts @@ -149,17 +149,14 @@ export class MatTableDataSource extends DataSource { let valueB = this.sortingDataAccessor(b, active); // If there are data in the column that can be converted to a number, - // it must be ensured that the rest of the data is of the same type so as not to order incorrectly. + // it must be ensured that the rest of the data + // is of the same type so as not to order incorrectly. const valueAType = typeof valueA; const valueBType = typeof valueB; if (valueAType !== valueBType) { - if (valueAType === 'number'){ - valueA += ''; - } - if (valueBType === 'number'){ - valueB += ''; - } + if (valueAType === 'number'){ valueA += ''; } + if (valueBType === 'number'){ valueB += ''; } } // If both valueA and valueB exist (truthy), then compare the two. Otherwise, check if From b6743ea2e0b61b2539fd7f18e8df6c5f850ce9a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Est=C3=A9vez=20Rocha?= Date: Wed, 5 Aug 2020 16:47:59 +0200 Subject: [PATCH 8/9] fix(material/table): Sort correctly Correctly sort the data in a column when it contains string data and numeric values. If this occurs, numeric values are converted to text --- src/material/table/table-data-source.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/material/table/table-data-source.ts b/src/material/table/table-data-source.ts index 74c2b132ef3b..8d3d834d44fd 100644 --- a/src/material/table/table-data-source.ts +++ b/src/material/table/table-data-source.ts @@ -149,14 +149,14 @@ export class MatTableDataSource extends DataSource { let valueB = this.sortingDataAccessor(b, active); // If there are data in the column that can be converted to a number, - // it must be ensured that the rest of the data + // it must be ensured that the rest of the data // is of the same type so as not to order incorrectly. const valueAType = typeof valueA; const valueBType = typeof valueB; if (valueAType !== valueBType) { - if (valueAType === 'number'){ valueA += ''; } - if (valueBType === 'number'){ valueB += ''; } + if (valueAType === 'number') { valueA += ''; } + if (valueBType === 'number') { valueB += ''; } } // If both valueA and valueB exist (truthy), then compare the two. Otherwise, check if From 0b10a75bee1b2d2141bcc054c3dfcef5640a5225 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Est=C3=A9vez=20Rocha?= Date: Wed, 5 Aug 2020 16:47:59 +0200 Subject: [PATCH 9/9] fix(table): correctly sort columns with mixed data types --- src/material/table/table-data-source.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/material/table/table-data-source.ts b/src/material/table/table-data-source.ts index 74c2b132ef3b..8d3d834d44fd 100644 --- a/src/material/table/table-data-source.ts +++ b/src/material/table/table-data-source.ts @@ -149,14 +149,14 @@ export class MatTableDataSource extends DataSource { let valueB = this.sortingDataAccessor(b, active); // If there are data in the column that can be converted to a number, - // it must be ensured that the rest of the data + // it must be ensured that the rest of the data // is of the same type so as not to order incorrectly. const valueAType = typeof valueA; const valueBType = typeof valueB; if (valueAType !== valueBType) { - if (valueAType === 'number'){ valueA += ''; } - if (valueBType === 'number'){ valueB += ''; } + if (valueAType === 'number') { valueA += ''; } + if (valueBType === 'number') { valueB += ''; } } // If both valueA and valueB exist (truthy), then compare the two. Otherwise, check if