diff --git a/src/material/legacy-table/table-data-source.spec.ts b/src/material/legacy-table/table-data-source.spec.ts index 3ebe1af4cb46..eca05ba77a7c 100644 --- a/src/material/legacy-table/table-data-source.spec.ts +++ b/src/material/legacy-table/table-data-source.spec.ts @@ -53,6 +53,20 @@ describe('MatTableDataSource', () => { testSortWithValues([3, 'apples', 'bananas', 'cherries', 'lemons', 'strawberries']); }); + it('should be able to correctly sort an array of strings and numbers with left zero', () => { + testSortWithValues([ + 3, + '001', + '2', + 4, + 'apples', + 'bananas', + 'cherries', + 'lemons', + 'strawberries', + ]); + }); + it('should unsubscribe from the re-render stream when disconnected', () => { const spy = spyOn(dataSource._renderChangesSubscription!, 'unsubscribe'); dataSource.disconnect(); diff --git a/src/material/table/table-data-source.ts b/src/material/table/table-data-source.ts index 318eaa83b76b..5fbf3e6fbefe 100644 --- a/src/material/table/table-data-source.ts +++ b/src/material/table/table-data-source.ts @@ -197,16 +197,17 @@ export class _MatTableDataSource< // 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. + // is of the same type so as not to order incorrectly + // return default value to not lose leading zeros. const valueAType = typeof valueA; const valueBType = typeof valueB; if (valueAType !== valueBType) { if (valueAType === 'number') { - valueA += ''; + valueA = (a as unknown as Record)[active]; } if (valueBType === 'number') { - valueB += ''; + valueB = (b as unknown as Record)[active]; } }