Alphabetical sorts

Ron Adam rrr at ronadam.com
Tue Oct 17 12:21:11 EDT 2006


Neil Cerutti wrote:
> On 2006-10-17, Ron Adam <rrr at ronadam.com> wrote:
>> Neil Cerutti wrote:
>>> On 2006-10-16, Ron Adam <rrr at ronadam.com> wrote:
>>>> I have several applications where I want to sort lists in
>>>> alphabetical order. Most examples of sorting usually sort on
>>>> the ord() order of the character set as an approximation.
>>>> But that is not always what you want.
>>> Check out strxfrm in the locale module.
>> It looks to me this would be a good candidate for a
>> configurable class. Something preferably in the string module
>> where it could be found easier.
>>
>> Is there anyway to change the behavior of strxfrm or strcoll?
>> For example have caps before lowercase, instead of after?
> 
> You can probably get away with writing a strxfrm function that
> spits out numbers that fit your definition of sorting.


Since that function is 'C' coded in the builtin _locale, it can't be modified by 
python code.

Looking around some more I found the documentation for the corresponding C 
functions and data structures. It looks like python may just wrap these.

    http://opengroup.org/onlinepubs/007908799/xbd/locale.html


Here's one example of how to rewrite the Unicode collate in python.

    http://jtauber.com/blog/2006/01

I haven't tried changing it's behavior, but I did notice it treats words with 
hyphen in them differently than strxfrm.



Here's one way to change caps order.

a = ["Neil", "Cerutti", "neil", "cerutti"]

locale.setlocale(locale.LC_ALL, '')
tmp = [x.swapcase() for x in a]
tmp.sort(key=locale.strxfrm)
tmp = [x.swapcase() for x in tmp]
print tmp


['Cerutti', 'cerutti', 'Neil', 'neil']



Cheers,
    Ron





More information about the Python-list mailing list