Blog·Craft·

City search ranks, it does not alphabetize

/city?q= orders results by relevance, so the biggest match leads even when the alphabet disagrees.

The alphabet does not know which Charleston you meant.

GET /city?q=char&country=US&limit=5
{
  "cities": [
    { "name": "Charlotte", "state": "NC", "population": 943476 },
    { "name": "Charleston", "state": "SC", "population": 157665 },
    { "name": "Charleston", "state": "WV", "population": 46482 },
    { "name": "Charlottesville", "state": "VA", "population": 44767 },
    { "name": "Charleston", "state": "IL", "population": 17361 }
  ]
}

Alphabetically, Charleston comes before Charlotte. In the response, Charlotte leads. cities is a ranked array for a picker, not a sorted dump of everything that matches the prefix. Prefix match still has to hold, population only breaks the tie among real hits.

Ranked for a picker

limit caps the array at whatever a dropdown can actually show, five results, ten, whatever the UI has room for. Population is the signal that fills those slots, because a typeahead field only gets one guess before someone stops typing and picks. Five Charlestons and one Charlotte, and the biggest one still leads.

Try it

Call ?q=char&country=US and watch population decide the order. The alphabet doesn't get a vote.