Skip to content

Sort and page countries by their name - #1568

Merged
frenck merged 1 commit into
mainfrom
frenck/fix-countries-name-order
Oct 1, 2026
Merged

frenck merged 1 commit into
mainfrom
frenck/fix-countries-name-order

Conversation

@frenck

@frenck frenck commented Oct 1, 2026

Copy link
Copy Markdown
Owner

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=name sorts on the code. The client resolved the codes to names and re-sorted afterwards, but:

  • reverse=True was ignored, the local sort was always ascending.
  • limit and offset were 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 (nl next to NL), which shows up as duplicate names. That deserves its own fix.

Type of change

  • Dependency or tooling upgrade
  • Bugfix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Deprecation (replaces or removes a feature, with a migration path)
  • Breaking change (a fix or feature that changes existing behavior)
  • Code quality, refactor, or test-only change
  • Documentation only

Additional information

  • This PR fixes or closes issue: fixes # None
  • This PR is related to: None
  • Link to a separate documentation pull request: None

Checklist

  • I fully understand the code in this pull request and can explain every line, including any AI-assisted changes.
  • The change is covered by tests, and poetry run pytest passes locally. A pull request cannot be merged unless CI is green.
  • poetry run prek run --all-files passes (lint, format, and type checks).
  • No commented-out or dead code is left in the pull request.

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".
@frenck frenck added the bugfix Inconsistencies or issues which will cause a problem for users or implementers. label Oct 1, 2026
@frenck
frenck merged commit e3ca595 into main Oct 1, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Inconsistencies or issues which will cause a problem for users or implementers.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant