python sorting 2dim. array ?

F

fredo66

hello,
Can someone help me with this:
I have a array like this

list[rowindex][colomindex]

where rows are the records and colom the fields. If I use the .sort()
method on 'list' the data is sorted on the items of the first colom.
But I want to sort on the second colom as first (and as second
sortfield the first colom).

What is the shortest code for this pls ?

(all fields are text value, first colom is name, second category)
 
P

Peter Otten

hello,
Can someone help me with this:
I have a array like this

list[rowindex][colomindex]

where rows are the records and colom the fields. If I use the .sort()
method on 'list' the data is sorted on the items of the first colom.
But I want to sort on the second colom as first (and as second
sortfield the first colom).

What is the shortest code for this pls ?

(all fields are text value, first colom is name, second category)
items = [(1,2), (2,2), (2,1)]
items.sort(lambda x, y: cmp(x[1::-1], y[1::-1]))
items
[(2, 1), (1, 2), (2, 2)]

If you want something more efficient, see

http://www.python.org/doc/faq/progr...-can-you-do-a-schwartzian-transform-in-python

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top