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(table): data source should sort empty values correctly #8698

Merged
merged 1 commit into from
Jan 23, 2018

Conversation

andrewseguin
Copy link
Contributor

@andrewseguin andrewseguin commented Nov 29, 2017

Also takes the sort function and adds it as an override-able predicate for users to inject their own method of sorting. It takes a MatSort rather than the active and directive properties in hopes that it's more stable in case we add properties to MatSort later on. Open to opinions on whether that's wise or not

Fixes #8485

@andrewseguin andrewseguin added P2 The issue is important to a large percentage of users, with a workaround pr: needs review labels Nov 29, 2017
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Nov 29, 2017
@mcalmus
Copy link
Contributor

mcalmus commented Nov 29, 2017

This line:
+ comparatorResult = valueA < valueB ? -1 : 1;
Assumes that if valueA isn't smaller than valueB it's bigger. If valueA and valueB are the same you could end up with weird results.

@andrewseguin
Copy link
Contributor Author

Good call - forgot about the case of them being equal and remaining at 0. Thanks

@andrewseguin andrewseguin added target: minor This PR is targeted for the next minor release target: patch This PR is targeted for the next patch release and removed target: minor This PR is targeted for the next minor release labels Dec 11, 2017
}

/** Whether the provided value is considered a number. */
export function isNumberValue(value: any): boolean {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this should be exposed as a public API under coercion. Mark this as internal?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marked as docs-private

* Whether the provided value is considered a number.
* @docs-private
*/
export function isNumberValue(value: any): boolean {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to prefix with an _ to make it completely clear that this is not a public API (rather than being public and undocumented)

@@ -97,22 +99,51 @@ export class MatTableDataSource<T> implements DataSource<T> {
*/
sortingDataAccessor: ((data: T, sortHeaderId: string) => string|number) =
(data: T, sortHeaderId: string): string|number => {
const value: any = data[sortHeaderId];
let value: any = data[sortHeaderId];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const

return value;
}
/**
* Returns a sorted array of the provided data according to the state of the MatSort. Called
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Returns a sorted array..." -> "Gets a sorted copy of the data array based on the state of the MatSort"

* @param data The array of data that should be sorted.
* @param sort The connected MatSort that holds the current sort state.
*/
sortPredicate: ((data: T[], sort: MatSort) => T[]) = (data: T[], sort: MatSort): T[] => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A "predicate" is a function that returns true or false, so this isn't quite the right name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any suggestions? Just sort?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sgtm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh duh, sort is an input for the MatSort. Hmm, what about...sortData? Kinda fits with the function using sortDataAccessor

* @param data The array of data that should be sorted.
* @param sort The connected MatSort that holds the current sort state.
*/
sortPredicate: ((data: T[], sort: MatSort) => T[]) = (data: T[], sort: MatSort): T[] => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new API would really make this a minor release change

comparatorResult = -1;
}
} else if (valueA) {
comparatorResult = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment that explains how you treat null/undefined for sorting (that they come first)

@andrewseguin andrewseguin force-pushed the fix-datasource-empty-sort branch 3 times, most recently from 36390a9 to 4422bde Compare January 18, 2018 20:46
@andrewseguin
Copy link
Contributor Author

Ready for re-review

@andrewseguin andrewseguin added target: minor This PR is targeted for the next minor release and removed target: patch This PR is targeted for the next patch release labels Jan 18, 2018
Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Splaktar Splaktar added this to the 5.2 milestone Apr 29, 2018
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement P2 The issue is important to a large percentage of users, with a workaround target: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Table] MatTableDataSource does not properly sort null/undefined values
5 participants