How to Sort this array
Tjabo Kloppenburg
tjabo.kloppenburg at unix-ag.org
Sat Oct 26 04:51:48 EDT 2002
> How to sort an array with numbers like 021025_11441 in it? It needs to be
> sorted first by the number before '_', then sorted by the number after '_'?
> How to do it using a customized sort function?
start you interpreter. next type:
>>> print [].sort.__doc__
L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1
so you need a function returning -1, 0 or 1..
def myCmp( x, y ):
x_s = map( int, x.split("_") )
y_s = map( int, y.split("_") )
# now we have to lists with two integers in each list.
# now just compare the ints an return -1, 0, or 1...
...
have fun... :-)
tk.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20021026/ae6f74e2/attachment.sig>
More information about the Python-list
mailing list