def sort_key(s):
 global sort_key
 try:
  from gluon.contrib.pyuca import unicode_collator
  unicode_sort_key = unicode_collator.sort_key
  sort_key = (lambda s: unicode_sort_key((to_unicode(s, 'utf-8') if isinstance(s, str) else s)))
 except:
  sort_key = (lambda s: (to_unicode(s, 'utf-8') if isinstance(s, str) else s).lower())
 return sort_key(s)

def sort_key ( s ) : global sort_key try : from gluon . contrib . pyuca import unicode_collator unicode_sort_key = unicode_collator . sort_key sort_key = ( lambda s : unicode_sort_key ( ( to_unicode ( s , 'utf-8' ) if isinstance ( s , str ) else s ) ) ) except : sort_key = ( lambda s : ( to_unicode ( s , 'utf-8' ) if isinstance ( s , str ) else s ) . lower ( ) ) return sort_key ( s )
unicode collation algorithm is used for utf-8 and unicode strings sorting and for utf-8 strings comparison note: pyuca is a very memory cost module! it loads the whole "allkey .