考虑区域设置的排序列表(瑞典排序)

时间:2022-06-20 20:27:44

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):

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

  • Wa
  • Vb
  • VB
  • Wc
  • 厕所
  • Vd
  • VD

it should be (new definition for Swedish ordering):

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

  • Vb
  • VB
  • Vd
  • 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..

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

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.

现在,我想要的是这些的组合,以便V / W和å,ä,ö的排序是正确的。更确切地说。我希望Ordering One尊重语言环境。然后在每个元组中使用第二个项(对象id),我可以在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?

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

2 个解决方案

#1


8  

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.

当你在Ubuntu-10.04上的例子中运行LC_ALL = sv_SE.UTF-8排序时,它会在Vb之前出现Wa(“旧方式”),所以Ubuntu似乎不同意“新方式”。由于PostgreSQL依赖于操作系统,因此它的行为与给定相同lc_collat​​e的操作系统的行为相同。

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.

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

#2


0  

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.

此解决方案很复杂,因为key = locale.strxfrm适用于单个列表和字典,但不适用于列表或元组列表。

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

从Py2 - > Py3的变化:使用locale.setlocale(locale.LC_ALL,'')和key ='locale.strxfrm'(而不是'cmp = locale.strcoll')。

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

def locTupSorter(uLot):
    "Locale-wise list of tuples sorter - works with most European languages"
    import locale
    locale.setlocale(locale.LC_ALL, '') # get current locale

    dicTups = dict(uLot)          # list of tups to unsorted dictionary
    ssList = sorted(dicTups, key=locale.strxfrm)
    sLot = []
    for i in range(len(ssList)):  # builds a sorted list of tups 
        tfLot = ()
        elem = ssList[i]          # creates tuples for list
        tfLot = (elem, dicTups[elem])
        sLot.append(tfLot)        # creates sorted list of tuples
    return(sLot)


print("list_of_tuples=\n", list_of_tuples)
sortedLot = locTupSorter(list_of_tuples)
print("sorted list of tuples=\n",sortedLot)

#1


8  

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.

当你在Ubuntu-10.04上的例子中运行LC_ALL = sv_SE.UTF-8排序时,它会在Vb之前出现Wa(“旧方式”),所以Ubuntu似乎不同意“新方式”。由于PostgreSQL依赖于操作系统,因此它的行为与给定相同lc_collat​​e的操作系统的行为相同。

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.

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

#2


0  

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.

此解决方案很复杂,因为key = locale.strxfrm适用于单个列表和字典,但不适用于列表或元组列表。

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

从Py2 - > Py3的变化:使用locale.setlocale(locale.LC_ALL,'')和key ='locale.strxfrm'(而不是'cmp = locale.strcoll')。

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

def locTupSorter(uLot):
    "Locale-wise list of tuples sorter - works with most European languages"
    import locale
    locale.setlocale(locale.LC_ALL, '') # get current locale

    dicTups = dict(uLot)          # list of tups to unsorted dictionary
    ssList = sorted(dicTups, key=locale.strxfrm)
    sLot = []
    for i in range(len(ssList)):  # builds a sorted list of tups 
        tfLot = ()
        elem = ssList[i]          # creates tuples for list
        tfLot = (elem, dicTups[elem])
        sLot.append(tfLot)        # creates sorted list of tuples
    return(sLot)


print("list_of_tuples=\n", list_of_tuples)
sortedLot = locTupSorter(list_of_tuples)
print("sorted list of tuples=\n",sortedLot)