473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

sort accented string

Hello,

I'm french and I have a small sorting problem with python (and zope's
zcatalog):

In a python shell python, try :

'é' > 'z'

The answer is true. Then if you try

test=['a','b','f','é','z']
print test.sort()

You almost get :

['a','b','f','z','é']

That's not what we want :-)

Any help will be greatly appriciated.

PS : I got the same behaviour with zope's zcatalog during a sort_on request.
Even if I start zope with the correct linux locale (fr_FR).

THX

Laurent.
Jul 18 '05 #1
5 4848
Laurent wrote:
I'm french and I have a small sorting problem with python (and zope's


Try locale.strcoll():
import locale
locale.setlocale(locale.LC_ALL, "fr_FR") 'fr_FR' test = list("abéfgz")
test.sort()
test ['a', 'b', 'f', 'g', 'z', '\xe9'] test.sort(locale.strcoll)
test ['a', 'b', '\xe9', 'f', 'g', 'z']


Peter

Jul 18 '05 #2
Thank you very much Peter, that's a really great tip. You should try to
publish this one on the Internet 'cause I didn't find any web site
explaining this.

Bye.

Laurent

Peter Otten wrote:
Laurent wrote:
I'm french and I have a small sorting problem with python (and zope's


Try locale.strcoll():
import locale
locale.setlocale(locale.LC_ALL, "fr_FR") 'fr_FR' test = list("abéfgz")
test.sort()
test ['a', 'b', 'f', 'g', 'z', '\xe9'] test.sort(locale.strcoll)
test ['a', 'b', '\xe9', 'f', 'g', 'z']


Peter


Jul 18 '05 #3
Peter what about using this kind of tip with cmp() built-in ??

def myCompare(self,a,b):
cmp(a,b)

with a and b being french accented chars ???

Any idea ??

Thanks again.

Peter Otten wrote:
Laurent wrote:
I'm french and I have a small sorting problem with python (and zope's


Try locale.strcoll():
import locale
locale.setlocale(locale.LC_ALL, "fr_FR") 'fr_FR' test = list("abéfgz")
test.sort()
test ['a', 'b', 'f', 'g', 'z', '\xe9'] test.sort(locale.strcoll)
test ['a', 'b', '\xe9', 'f', 'g', 'z']


Peter


Jul 18 '05 #4
Laurent wrote:
Peter what about using this kind of tip with cmp() built-in ??

def myCompare(self,a,b):
cmp(a,b)

with a and b being french accented chars ???

Any idea ??


From the documentation of the locale module:

"""
strxfrm(string)

Transforms a string to one that can be used for the built-in function cmp(),
and still returns locale-aware results. This function can be used when the
same string is compared repeatedly, e.g. when collating a sequence of
strings.
"""

Peter

Jul 18 '05 #5
Peter Otten wrote:
Laurent wrote:

Peter what about using this kind of tip with cmp() built-in ??

def myCompare(self,a,b):
cmp(a,b)

with a and b being french accented chars ???

Any idea ??

From the documentation of the locale module:

"""
strxfrm(string)

Transforms a string to one that can be used for the built-in function cmp(),
and still returns locale-aware results. This function can be used when the
same string is compared repeatedly, e.g. when collating a sequence of
strings.
"""

Peter

Note however that you need to transform both strings being compared...
e.g., with a french locale:
'd' < '\xe9' True 'f' < '\xe9' True 'd' < locale.strxfrm('\xe9') False 'f' < locale.strxfrm('\xe9') False locale.strxfrm('f') < locale.strxfrm('\xe9') False locale.strxfrm('d') < locale.strxfrm('\xe9')

True

David
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: googleboy | last post by:
I didn't think this would be as difficult as it now seems to me. I am reading in a csv file that documents a bunch of different info on about 200 books, such as title, author, publisher, isbn,...
6
by: Al Newton | last post by:
I want to use STL's sort algorithm to sort a string vector. Some of the strings are fairly long (300 to 400 chars) and the vector isn't small (5,000 to 10,000 elements). Naturally, sorting time...
8
by: lok | last post by:
i have a class: template <class T1, class T2> class CPairMapping { public: typedef std::pair<T1, T2> ValuePair_t; typedef std::vector<ValuePair_t> ValueList_t; typedef std::binary_function<...
9
by: David | last post by:
Hi all, I have an array of strings. For example... = "entry1 first entry"; = "entry2 second entry"; = "entry3 third entry"; etc... How can I sort this array by the number after "entry"?
1
by: anonieko | last post by:
I have a directory of files and I want to sort in in Array List in a customized way. How do I do that? > > > > > > class main {
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
12
by: aparnakakkar2003 | last post by:
can any one tell me if I give the followiing string in input: ABC abc BBC then how I can get ABC abc BBC
1
by: HaifaCarina | last post by:
these are the loops i used: for (h = 0; h<name.length;h++) { highestOrder = Integer.valueOf(name.charAt(0)); highestName = name; for (int count...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.