kentcdodds / match-sorter

Simple, expected, and deterministic best-match sorting of an array in JavaScript

Home Page:https://npm.im/match-sorter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to ignore caseRank while sorting?

mrceperka opened this issue Β· comments

Hello πŸ‘‹,
I've got a question and could not find the solution on the internet, so I ask here.

  • match-sorter version: 4.2.1
  • node version: 12.18.4
  • npm (or yarn) version: yarn 1.22.4

What you did: https://codesandbox.io/s/react-codesandbox-forked-t5tb4?file=/src/index.js
What happened: Items are sorted the way I do not expect/want.

Reproduction repository: see sandbox please

Problem description:
I want to sort items alphabetically, without additional case ranking. Case ranking causes unexpected (for me) sort.

Suggested solution:
Maybe, there already is an option to avoid this. But i was unable to find it. It would be great, if i could "just" sort by value, without case ranking consideration.

Maybe something like this could be used: https://github.com/mrceperka/match-sorter/blob/match-sorter-possible-fix/src/index.js#L135

Thanks πŸ™

Hi @mrceperka,

Could you call toLowerCase on the input and the values using the "Property Callbacks" feature shown in the README?

const list = [{name: 'Janice'}, {name: 'Fred'}, {name: 'George'}, {name: 'Jen'}]
const input = 'J'
matchSorter(list, input.toLowerCase(), {keys: [item => item.name.toLowerCase()]})
// [{name: 'Janice'}, {name: 'Jen'}]

Ah, that's a bummer. I think I actually ran into this same problem yesterday: https://kifs.netlify.app/

Those should be sorted alphabetically by default, but the three with ' show up first.

Hmmm.... I think it'd be even better if we could only consider alphabetical characters when matching case πŸ€”

Would you be willing to add a failing test for this?

@kentcdodds Sure thing, please, check #106

I think it'd be even better if we could only consider alphabetical characters when matching case

  • I would rather have this as an option
  • I would leave sorting to JS.sort() and baseSort if provided

I normally don't like logical options like this, but I don't think there's a reasonable alternative. Would you like to implement your suggestion?

Thanks!

@kentcdodds Yes, I will implement it. I will update existing PR asap.

PR here: #106

@kentcdodds bad news, sorting alphabetically be default breaks 12 tests. I am afraid that it can't be done by default without bumping major version...

I figured. I don't mind bumping the major version, bit I'm curious how they break and whether the breakage is desirable.

It would be best if you could clone PR branch, uncomment that test case and run npm test... Or I can paste log/screenshot here.

πŸŽ‰ This issue has been resolved in version 5.0.0 πŸŽ‰

The release is available on:

Your semantic-release bot πŸ“¦πŸš€