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

Further improve sorting algorithm to put undefined values at end of list #376

Merged
merged 4 commits into from Jun 12, 2020

Conversation

snewcomer
Copy link
Contributor

close #374

@snewcomer snewcomer self-assigned this Jun 12, 2020
@@ -44,7 +44,7 @@ module('Integration | Helper | {{sort-by}}', function(hooks) {
{{~/each~}}
`);

assert.equal(find('*').textContent.trim(), 'aAAabccb', 'sorts multiletter words');
assert.equal(find('*').textContent.trim(), 'AaaAbccB', 'sorts multiletter words');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We aren't splitting the strings up by individual char, so I actually think this is now "more" right.

return false;
} else if (val < 0) {
return true;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We shouldn't swap if val === 0, thus the need for this refactor

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah! Yes! This is what I could NOT seem to get my head around and fix. Yay!

@snewcomer
Copy link
Contributor Author

@jrjohnson Can I get your careful eye on this PR? Thank you!!

return false;
} else if (val < 0) {
return true;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah! Yes! This is what I could NOT seem to get my head around and fix. Yay!

@@ -17,6 +21,15 @@ function sortDesc(key, a, b) {
const aValue = get(a, key);
const bValue = get(b, key);

if (typeof bValue == 'undefined') {
Copy link
Contributor

Choose a reason for hiding this comment

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

This makes more sense to me than my fix.

// put aValue last
return 1;
}

if (aValue && bValue && aValue.toLowerCase && bValue.toLowerCase) {
Copy link
Contributor

Choose a reason for hiding this comment

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

aValue && bValue && can now be removed here as it was just to catch undefined. I think everything else can have toLowerCase check called on it.

// put aValue last
return 1;
}

if (aValue && bValue && aValue.toLowerCase && bValue.toLowerCase) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same, aValue && bValue && can go away now that undfined check is above.

@snewcomer snewcomer merged commit 6194f6a into master Jun 12, 2020
@snewcomer snewcomer deleted the pr/374 branch June 12, 2020 20:00
@snewcomer
Copy link
Contributor Author

Thank you for the awesome review!! 4.2.1 released!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants