?Does Python have sorting via "Dictionary Order"?
Bjorn Pettersen
bjorn at roguewave.com
Wed Jun 28 20:57:34 EDT 2000
Norman Shelley wrote:
>
> Does Python have sorting via "Dictionary Order"? This would be a very
> handy addition as an option to list's sort() function.
>
> This is defined by D. Richard Hipp as:
> "The case of alphabetic characters is ignored, except to break ties.
> Thus "B"
> comes before "b" but after "a". Also, integers embedded in the strings
> compare
> in numerical order. In other words, "x10y" comes after "x9y", not
> before it as
> it would when using strcmp().
Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> print [].sort.__doc__
L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0,
1
>>>
i.e. for a given list L, you can say "L.sort(f)" where f is a function
taking two arguments (a,b) and returns -1, 0, 1 if a<b, a==b, a>b
respectively. So all you have to do is implement f to compare two items
by Dictionary Order and you're done.
-- bjorn
More information about the Python-list
mailing list