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

Sort same ranked items alphabetically #64

Merged
merged 1 commit into from
Jun 6, 2019

Conversation

sdbrannum
Copy link
Contributor

What:

  1. Sort same ranked items alphabetically rather than by the index they were inputted.
  2. Updated tests.
  3. One new test.

Why:

Currently, if items are the same rank with the same key based index, the items are sorted based on their index in the starting input. This is fine in general but I believe it makes more sense to sort them alphabetically when they have the same ranking and also by length allowing the smaller words to float to the top and make filtering a progressive act.

Example:

search: 'ap'
input: ['wrapped','snap', 'pap']
old output: ['wrapped','snap', 'pap']
new output: ['pap', 'snap', 'wrapped']

How:

We can take advantage of the base String.prototype.localeCompare() to do a comparison while still accepting diacritics.

Checklist:

  • Documentation
  • Tests
  • Ready to be merged
  • Added myself to contributors table

Comments

This may need discussion and you may not like the approach or the idea at all, just let me know or if you find any issues.

Thanks!

Copy link
Owner

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

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

I think this is solid. I just want to make sure we're not missing something.

const same = aRank === bRank
if (same) {
if (aKeyIndex === bKeyIndex) {
return aIndex < bIndex ? aFirst : bFirst
return aRankedItem.localeCompare(bRankedItem)
Copy link
Owner

Choose a reason for hiding this comment

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

Is there ever a case when aRankedItem or bRankedItem could be anything but a string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tough to think through, the tests cover every situation i thought of.

  • No matches
  • Null values
  • Array of values
  • Array of objects with key
  • Nested objects with keys
  • Array of objects with multiple keys
  • Array of objects with key that points to array

An option as a "just-in-case" would be to add a typeof check on the xRankItems and add the indexes back in as a else clause but it would not meet 100% test branch coverage.

Copy link
Owner

Choose a reason for hiding this comment

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

I think we're probably ok. Fixing any issues would be simple and quick so we'll go with this. Thanks!

@kentcdodds kentcdodds merged commit 9888015 into kentcdodds:master Jun 6, 2019
@kentcdodds
Copy link
Owner

🎉 This PR is included in version 3.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

2 participants