考虑区域设置的元组排序列表(瑞典排序) [英] Sort list of tuples considering locale (swedish ordering)

查看:110
本文介绍了考虑区域设置的元组排序列表(瑞典排序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然PostgreSQL 8.4和Ubuntu 10.04无法处理更新的方式对瑞典语字母W和V进行排序。也就是说,它仍然是将它们命令为同样的字母(瑞典订购的旧定义):




  • Wa

  • Vb

  • Wc

  • Vd



应该是(瑞典订购的新定义):




  • Vb

  • Vd

  • Wa

  • Wc



我需要订购这正是我正在构建的Python / Django网站。我已经尝试过各种方法,只需使用* values_list *命令从Django QuerySet创建的元组列表。但由于瑞典语å,ä和ö信件需要正确订购。现在我有一个或另一个方式都不是两个..

  list_of_tuples = [(u'Wa',1) (u'Vb',2),(u'Wc',3),(u'Vd',4),(u'oa',5),(u'aa',6),(u'aa' ,7)] 

打印'##########订购一个#############'
ordered_list_one = sorted(list_of_tuples ,key = lambda t:tuple(t [0] .lower()))
在ordered_list_one中的项目:
打印项目[0]

打印'#### ###### Ordering Two #############'
locale.setlocale(locale.LC_ALL,sv_SE.utf8)
list_of_names = [u'哇',你'''''''''''''''''' b $ b在ordered_list_two中的项目:
打印项

示例给出:

  ##########订购一个############# 
Vb
Vd
Wa
Wc
äa
Åa
Öa
##########订购两个## ############
Wa
Vb
Wc
Vd
Åa
äa
Öa

现在,我想要是这些的组合,以便V / W和å,ä,ö排序是正确的。更精确我希望订购One 尊重区域设置。然后在每个元组中使用第二个项目(对象id),我可以在Django中获取正确的对象。



我开始怀疑这是可能的吗?将PostgreSQL升级到更好的处理归类的新版本,然后在Django中使用原始SQL?

解决方案

运行<$您在Ubuntu-10.04上的示例中的c $ c> LC_ALL = sv_SE.UTF-8 sort ,它出现在Vb之前的Wa(旧方式),所以Ubuntu似乎不同意新路。
由于PostgreSQL依赖于操作系统,因此它的行为与使用相同lc_collat​​e的操作系统相同。



实际上debian中有一个补丁glibc与此特定排序问题有关:
http://sourceware.org/bugzilla /show_bug.cgi?id=9724
但是它被反对并且不被接受。如果您仅在您管理的系统上需要此行为,则仍然可以将修补程序更改应用于/ usr / share / i18n / locales / sv_SE,并通过运行 locale-gen sv_SE来重建se_SV区域设置。 UTF-8 。或者更好的是,创建您自己的替代语言环境,以避免与原始的错误。


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

I need to order this correctly for a Python/Django website I'm building. I have tried various ways to just order a list of tuples created from a Django QuerySet using *values_list*. But since it's Swedish also å, ä and ö letters needs to be correctly ordered. Now I have either one or the other way both not both..

list_of_tuples = [(u'Wa', 1), (u'Vb',2), (u'Wc',3), (u'Vd',4), (u'Öa',5), (u'äa',6), (u'Åa',7)]

print '########## Ordering One ##############'
ordered_list_one = sorted(list_of_tuples, key=lambda t: tuple(t[0].lower()))
for item in ordered_list_one:
    print item[0]

print '########## Ordering Two ##############'
locale.setlocale(locale.LC_ALL, "sv_SE.utf8")
list_of_names = [u'Wa', u'Vb', u'Wc', u'Vd', u'Öa', u'äa', u'Åa']
ordered_list_two = sorted(list_of_names, cmp=locale.strcoll)
for item in ordered_list_two:
    print item

The examples gives:

########## Ordering One ##############
Vb
Vd
Wa
Wc
äa
Åa
Öa
########## Ordering Two ##############
Wa
Vb
Wc
Vd
Åa
äa
Öa

Now, What I want is a combination of these so that both V/W and å,ä,ö ordering are correct. To be more precise. I want 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?

解决方案

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.

这篇关于考虑区域设置的元组排序列表(瑞典排序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆