- You have a list of dozens, hundreds, or thousands of items
- You want to filter and sort those items intelligently (maybe you have a filter input for the user)
- You want simple, expected, and deterministic sorting of the items (no fancy math algorithm that fancily changes the sorting as they type)
This follows a simple and sensible (user friendly) algorithm that makes it easy for you to filter and sort a list of items based on given input. Items are ranked based on sensible criteria that result in a better user experience.
To explain the ranking system, I'll use countries as an example:
- CASE SENSITIVE EQUALS: Case-sensitive equality trumps all. These will be first. (ex.
France
would matchFrance
, but notfrance
) - EQUALS: Case-insensitive equality (ex.
France
would matchfrance
) - STARTS WITH: If the item starts with the given value (ex.
Sou
would matchSouth Korea
orSouth Africa
) - WORD STARTS WITH: If the item has multiple words, then if one of those words starts with the given value (ex.
Repub
would matchDominican Republic
) - CASE STARTS WITH: If the item has a defined case (
camelCase
,PascalCase
,snake_case
orkebab-case
), then if one of the parts starts with the given value (ex.kingdom
would matchunitedKingdom
orunited_kingdom
) - CASE ACRONYM If the item's case matches the synonym (ex.
uk
would matchunited-kingdom
orUnitedKingdom
) - CONTAINS: If the item contains the given value (ex.
ham
would matchBahamas
) - ACRONYM: If the item's acronym is the given value (ex.
us
would matchUnited States
) - SIMPLE MATCH: If the item has letters in the same order as the letters of the given value (ex.
iw
would matchZimbabwe
, but notKuwait
because it must be in the same order). Furthermore, if the item is a closer match, it will rank higher (ex.ua
matchesUruguay
more closely thanUnited States of America
, thereforeUruguay
will be ordered beforeUnited States of America
)
This ranking seems to make sense in people's minds. At least it does in mine. Feedback welcome!
MIT