kentcdodds / match-sorter

Simple, expected, and deterministic best-match sorting of an array in JavaScript
https://npm.im/match-sorter
MIT License
3.73k stars 129 forks source link

Sort same ranked items alphabetically #64

Closed sdbrannum closed 5 years ago

sdbrannum commented 5 years ago

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:

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!

kentcdodds commented 5 years ago

:tada: This PR is included in version 3.1.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: