Sort list of tuples considering locale (swedish ordering)

匿名 (未验证) 提交于 2019-12-03 08:44:33

问题:

Apparently PostgreSQL 8.4 and Ubuntu 10.04 cannot handle the updated way to sort W and V for Swedish alphabet. That is, it's still ordering them as the same letter like this (old definition for Swedish ordering):

  • Wa
  • Vb
  • Wc
  • Vd

it should be (new definition for Swedish ordering):

  • Vb
  • Vd
  • Wa
  • Wc

The examples gives:

Ordering One to respect locale. By then using the second item (object id) in each tuple I could fetch the correct object in Django.

I'm starting to doubt that this will be possible? Would upgrading PostgreSQL to a newer version that handles collations better and then use raw SQL in Django be possible?

回答1:

When running LC_ALL=sv_SE.UTF-8 sort on your example on Ubuntu-10.04, it comes out with Wa before Vb (the "old way"), so Ubuntu does not seem to agree with the "new way". Since PostgreSQL relies on the operating system for this, it will behave just the same as the OS given the same lc_collate.

There is actually a patch in debian glibc related to this particular sort issue: http://sourceware.org/bugzilla/show_bug.cgi?id=9724 But it was objected to and not accepted. If you only need this behavior on a system you administer, you can still apply the change of the patch to /usr/share/i18n/locales/sv_SE and rebuild the se_SV locale by running locale-gen sv_SE.UTF-8. Or better yet, create your own alternative locale derived from it to avoid messing with the original.



回答2:

This solution is complex because key=locale.strxfrm works fine with single lists and dictionaries, but not with lists of lists or lists of tuples.

Changes from Py2 -> Py3 : use locale.setlocale(locale.LC_ALL, '') and key='locale.strxfrm' (instead of 'cmp=locale.strcoll').



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!