Rank autocomplete suggestions
Order matching suggestions by match quality, popularity, and deterministic tie-breakers.Description
Requirements
The solution exports a callable function named rankAutocompleteSuggestions.
The query and labels are compared using trimmed, lowercase text.
Match priority is exact label, full-label prefix, any-word prefix, then substring.
Suggestions whose normalized labels do not contain the query are excluded.
Within the same match tier, higher popularity is ranked first.
Remaining ties use normalized label ascending and then original input order.
At most limit matching suggestions are returned.
Returned suggestion objects are new copies and caller-owned data is not mutated.
Match-quality ranking
rankAutocompleteSuggestions('san', suggestions, 4)Exact 'San', then 'San Francisco', then 'Hotel San Remo', then 'Artisan Market'.
Constraints
limit is an integer between 0 and 100Every suggestion has a unique string id, non-empty label, and finite non-negative popularityWords are separated by one or more whitespace charactersThe input may contain up to 20000 suggestions
Hints
Hint 1
Normalize the query and candidate labels once, then assign each matching candidate to a match tier.
Hint 2
Sort by a tuple of tier, descending popularity, normalized label, and original position.
Hint 3
Apply the result limit after ranking, and return fresh suggestion objects.