473,804 Members | 2,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4883
Laurent wrote:
I'm french and I have a small sorting problem with python (and zope's


Try locale.strcoll( ):
import locale
locale.setlocal e(locale.LC_ALL , "fr_FR") 'fr_FR' test = list("abéfgz")
test.sort()
test ['a', 'b', 'f', 'g', 'z', '\xe9'] test.sort(local e.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.setlocal e(locale.LC_ALL , "fr_FR") 'fr_FR' test = list("abéfgz")
test.sort()
test ['a', 'b', 'f', 'g', 'z', '\xe9'] test.sort(local e.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.setlocal e(locale.LC_ALL , "fr_FR") 'fr_FR' test = list("abéfgz")
test.sort()
test ['a', 'b', 'f', 'g', 'z', '\xe9'] test.sort(local e.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
6213
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, date and several other bits of info too. I can do a simple sort over the first field (title as it turns out), and that is fine as far as it gets:
6
10486
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 is long. All I need sorted are the first 10 characters in the strings. Limiting the number of characters processed should speed things up considerably. Is there a way to do 1t? Thanks for any help ... Al
8
3509
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< ValuePair_t, ValuePair_t, bool> ValuePair_IsLess; void SortAscend(const ValuePair_IsLess& isLess_) {
9
3564
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
2366
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
4328
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 substring before a pattern), AFTER (return substring after a pattern), and BETWEEN (return substring between 2 patterns). My questions are: 1. Can any tell me how I can implement such functionality in C#? 2. Is it possible to add/include function...
48
4494
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 lot faster with both methods using .NET 1.1, that's why I am pretty sure its a (rather serious) bug. Below you can find C# test case that should allow you to reproduce this error, to run it you will need to put 2 data files into current directory...
12
4393
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
8776
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 = 0; count<name.length;count++) { if (Integer.valueOf(name.charAt(0)) < highestOrder)
0
9715
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10097
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9175
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7642
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.