Skip to content

How to sort collections based on current user locale on a Django site

I need to sort a collection of objects by a utf-8 string property (built via ActiveRecord). Currently the code is sorting by ASCII order via the order_by method, however this needs to be changed to locale.strcoll. Unfortunately using the built in locale functionality requires changing the culture for the entire application, not just the current request.

I’ve looked at the Babel library but it does not appear to provide the functionality I need.

The only other option I have been able to find is pyICU, however getting the ICU libraries installed in my environment will prevent this as a viable solution.

Are there any other options?



The solution I ended up taking is just converting the utf-8 string to ASCII and stripping the diacritics just for the sort operation. Not ideal but it ended up working for this specific case.

3 People found this is helpful