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 default sort comparator to logically handle null/undefined values #922

Conversation

chandlerprall
Copy link
Contributor

Fixes #920 by checking if sort values are null or undefined and handling those separately. This change does not handle all "invalid" comparisons (e.g. {} > {} is always false, and there are other cases) but I don't think the default comparator should incorporate the necessary logic to handle all of the edge cases (Symbol.toPrimitive and OrdinaryToPrimitive).

undefined sort values

Copy link
Contributor

@cjcenizal cjcenizal left a comment

Choose a reason for hiding this comment

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

Nice! This makes sense. Could you also add some test cases to cover this in comparators.test.js? And could you replace some of the firstNames in the table examples' data store with null and undefined so we can manually verify the sorting behavior in the browser?

@chandlerprall
Copy link
Contributor Author

@cjcenizal Added the test and updated docs data

Copy link
Contributor

@cjcenizal cjcenizal left a comment

Choose a reason for hiding this comment

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

🏀 LGTM! Just had one suggestion.

@@ -46,3 +46,10 @@ describe('comparators - property', () => {
});
});

describe('default comparator', () => {
test('null/undefined values are sorted to the end', () => {
const sorted = [undefined, '7', 3, undefined, 5, undefined].sort(Comparators.default());
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe change one of the undefined values to null to cover both cases?

Copy link
Contributor Author

@chandlerprall chandlerprall Jun 13, 2018

Choose a reason for hiding this comment

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

I was afraid the undefined nature of sorting null/undefined values would make this unstable, but apparently v8 prepares the array by moving all undefineds to the end of the unsorted array, so it's stable in node to check the null exists first.

@chandlerprall
Copy link
Contributor Author

jenkins test this

@chandlerprall chandlerprall merged commit 0be2cc5 into elastic:master Jun 13, 2018
@chandlerprall chandlerprall deleted the bug/920-inconsistent-sorted-undefined-values branch June 13, 2018 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants