Summarizing the doc and giving a more inductive explanation:
Before we can sort, we must be able to compare.
The default comparison between strings - using, for example, operators like < - does not care about the locale setting. To get such a comparison, we need the strcoll function from the same locale standard library module that you use to set the locale. It’s defined to implement a three-way comparison, like the old-style cmp function. (This way, a single ordinary function represents the logic for all the comparison operators). To use that as a key for sorting, we need the cmp_to_key adapter from functools, which then allows for writing e.g. sorted(my_data, key=functools.cmp_to_key(locale.strcoll)).
Hmm… possibly. The docs says that strcoll() compares two strings according to the current LC_COLLATE setting. As expected the character values returned from locale.strcoll() changes when I change the LC_COLLATE value.
Afaik (after a quick check in the cpython code) strcoll uses the general C-library function wcscoll – if that function is available – so this issue should be reproducible in a little C program.
(And it is reproducible. I just tried it with a slightlly modified version of the program of the stackoverflow post linked below.)
See also: https://github.com/python/cpython/blob/7dd3c2b80064c39f1f0ebbc1f8486897b3148aa5/Lib/test/test_locale.py#L349
I also tried the codecs module, tinkering with the encoding. But none of that leads to a correct sort order for Norwegian. The codecs work, but none of the special ones reflect the Bokmål order…So, codecs doesn’t help.
But… the stackoverlow post actually also gave a hint about a work-around. Those old post are still valuable! This work-around also happens to be available in Python. Since I didn’t know about this issue and work a lot with different languages, I decided to dig it out a bit more.
You can install PyICU (Python wrapper for Unicode tools) (only <= Python 3.11 unfortunately). Just do a regular pip install (after doing a brew install of icu if you don’t yet have that - see Pypi page).
(I also did the export PATH as indicated on the Pypi page, but this may not be needed for simple use in Python.)
Then:
>>> from icu import Locale, Collator
>>> loc = Locale("nb")
>>> loc.getDisplayName()
'Norwegian Bokmål'
>>> col = Collator.createInstance(loc) # a rule-based collator
>>> x = ['å', 'æ', 'ø']
>>> x.sort(key=col.getSortKey)
['æ', 'ø', 'å']
Also, note that this code does not care about any of the locale-related settings in the OS environment - everything is controlled by the icu calls in Python.