Repository navigation
Sort and page countries by their name - #1568
Merged
Merged
Conversation
The API only knows country codes, so "order by name" sorted and paged on the code. The client then re-sorted the page by name, but ignored reverse, and a page with a limit or offset held the wrong countries. Fetch the whole list when sorting by name, and sort and page it locally. Also sort accented names with their letter, so "Åland Islands" no longer comes after "Zimbabwe".
This was referenced Oct 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Breaking change
None.
countries()keeps its signature; when ordering by name (the default), it now returns the countries it promised: sorted by name, in the requested direction, and paged by name.Proposed change
Found in a review of the library. The API only knows country codes, so
countrycodes?order=namesorts on the code. The client resolved the codes to names and re-sorted afterwards, but:reverse=Truewas ignored, the local sort was always ascending.limitandoffsetwere applied by the API on the code order, before the names were known.countries(limit=3)returned the first three country codes (Andorra, United Arab Emirates, Afghanistan), sorted by name, not the first three countries.When ordering by name, the whole list (around 245 countries) is now fetched, sorted locally in the requested direction, and paged after that. Ordering by station count is left to the API, which gets that right.
While checking this against the live API, a second problem showed up: names with an accented first letter sorted after "Z". "Åland Islands" came after "Zimbabwe", and Côte d'Ivoire, Curaçao, Réunion, Saint Barthélemy and Türkiye were out of place too. Names are now compared without accents and ignoring case.
Live, after this change: the first three by name are Afghanistan, Albania, Algeria; reversed, Zimbabwe, Zambia, Yemen; the second page of three is American Samoa, Andorra, Angola.
Not part of this change: the API lists six countries twice, once with a lowercase code (
nlnext toNL), which shows up as duplicate names. That deserves its own fix.Type of change
Additional information
Checklist
poetry run pytestpasses locally. A pull request cannot be merged unless CI is green.poetry run prek run --all-filespasses (lint, format, and type checks).