# 
http://stackoverflow.com/questions/11121636/sorting-list-of-string-with-specific-locale-in-python

import icu   # PyICU
def sorted_strings(strings, locale=None):
    if locale is None:
       return sorted(strings)
    collator = icu.Collator.createInstance(icu.Locale(locale))
    return sorted(strings, key=collator.getSortKey)



On Friday, 10 February 2017 18:08:58 UTC+1, Mirek Zvolský wrote:
>
> ... or to any other special order.
>
> For me was creazy that I was not able to sort items (names) in IS_IN_DB 
> lists alphabetically.
> IS_IN_DB has lot of undocumented parameters but I think nothing can help.
>
> Of course when running from Postgres, you will receive records with proper 
> locale order.
> However with SQLite you need (maybe) a hacked and special compiled SQLite 
> to have correct order.
> Otherwise you start with uppercase ascii, then follow lowercase ascii, and 
> at the end are accented (non-ascii) characters. Crazy.
>
> Now I use this hack in my model:
>
> import locale
> locale.setlocale(locale.LC_ALL, 'cs_CZ.UTF-8')   # in multi-languages 
> environment use PyICU instead (thx Niphlod)
>
>
> class IS_IN_DB_(IS_IN_DB):
>     def build_set(self):
>         super(IS_IN_DB_, self).build_set()
>         records = [(lbl, self.theset[pos]) for pos, lbl in enumerate(self.
> labels)]
>         records.sort(key=lambda x: locale.strxfrm(x[0]))
>         self.labels = [rec[0] for rec in records]
>         self.theset = [rec[1] for rec in records]
>
>
> db.define_table('payment',
>     Field('idauth_user', 'reference auth_user', requires=IS_EMPTY_OR(
> IS_IN_DB_(db, db.auth_user.id, '%(nick)s'))),
>     ...)
>
>
>
> Not optimal, but works !
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to