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

Objects are always sorted by the last key instead of the matching key only (if ranks are same)

bernharduw opened this issue Β· comments

  • match-sorter version: v4.0.1
  • node version: v10.15.0
  • npm (or yarn) version: yarn v1.17.3

Relevant code or config

console.log(matchSorter([
        {country: 'Italy', counter: 3},
        {country: 'Italy', counter: 2},
        {country: 'Italy', counter: 1},
      ],
      'Italy',
      {keys: ['country', 'counter']}));
//  =>    {country: 'Italy', counter: 1}, {country: 'Italy', counter: 2}, {country: 'Italy', counter: 3}

What you did:
We're using match-sorter for searching through lists of real estate objects (pre-sorted with most recent ones first).

What happened:
When several items match and have the same rank, they are resorted in an unexpected order – the value of the last key.

Reproduction repository:
See code example above. I'll also create a test showing that behavior in the PR.

Problem description:
In v4.0, a change was introduced to sort results alphabetically. When using multiple keys for matching, the last key in the list is used for sorting equally ranked items, which leads to unexpected results.

Suggested solution:
Sort by the value of the key that actually matched instead of the last one.

This change was introduced by @sdbrannum and was released in 3.1.0. I think I agree with you @bernharduw, and the recommendation for people will be to sort things before sending them into match-sorter. Thanks for the suggestion and PR. I'll merge that and we'll make this a breaking change (because it is).

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

The release is available on:

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