- 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.
Francewould matchFrance, but notfrance) - EQUALS: Case-insensitive equality (ex.
Francewould matchfrance) - STARTS WITH: If the item starts with the given value (ex.
Souwould matchSouth KoreaorSouth Africa) - WORD STARTS WITH: If the item has multiple words, then if one of those words starts with the given value (ex.
Repubwould matchDominican Republic) - CASE STARTS WITH: If the item has a defined case (
camelCase,PascalCase,snake_caseorkebab-case), then if one of the parts starts with the given value (ex.kingdomwould matchunitedKingdomorunited_kingdom) - CASE ACRONYM If the item's case matches the synonym (ex.
ukwould matchunited-kingdomorUnitedKingdom) - CONTAINS: If the item contains the given value (ex.
hamwould matchBahamas) - ACRONYM: If the item's acronym is the given value (ex.
uswould matchUnited States) - SIMPLE MATCH: If the item has letters in the same order as the letters of the given value (ex.
iwwould matchZimbabwe, but notKuwaitbecause it must be in the same order). Furthermore, if the item is a closer match, it will rank higher (ex.uamatchesUruguaymore closely thanUnited States of America, thereforeUruguaywill 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