Skip to content
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

fix(material/table): sorting column breaks if contains a number with zero left #25444

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions src/material/legacy-table/table-data-source.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 4 additions & 3 deletions src/material/table/table-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>)[active];
}
if (valueBType === 'number') {
valueB += '';
valueB = (b as unknown as Record<string, any>)[active];
}
}

Expand Down