Sorting Objects by property using locale
Donn Ingle
donn.ingle at gmail.com
Fri Dec 21 07:44:04 EST 2007
Hi,
Well, I'm beat. I can't wrap my head around this stuff.
I need to create a list that will contain objects sorted by a "name"
property that is in the alphabetical order of the user's locale.
I used to have a simple list that I got from os.listdir and I could do this:
l = os.listdir(".")
l.sort(locale.strcoll)
Which would work fine.
But now I need to make objects (of different stripes) for each of the
filenames returned and then I want to collect them all into a main list and
have *that* main list be sorted.
I started some test code like this: I hope the odd characters show.
# -*- coding: utf8 -*-
class Test(object):
def __init__(self,nam):
self.name = nam
def __cmp__(self, other):
return cmp(self.name, other.name)
l = [ Test("ABILENE.ttf"), Test("Årgate.ttf"), Test("årse.ttf"),
Test("Ärt.ttf"), Test("MomentGothic.ttf"), Test("öggi.ttf"),
Test("Öhmygawd.ttf")]
import locale
l.sort() # won't work -> locale.strcoll)
for i in l: print i.name
Can anyone give me a leg-up?
\d
More information about the Python-list
mailing list