View Single Post
Old Feb 22nd, 2005, 3:04 AM   #1
k@t
Newbie
 
Join Date: Feb 2005
Posts: 1
Rep Power: 0 k@t is on a distinguished road
Help needed with sort()

Hi,

I have an array of arrays in the form of
list = [[3,'fork',0.3,1],[2,'fork,0.1,2],[3,'exec',0.2,2]]

The in-built sort(),list.sort() sorts on the first element, if the first elts are equal then it sorts on the second elt and so on...But i really dont want to search on the second elt if the first elts are equal...the 1-D lists shud be left in the same position i.e. i want the sorted list to be [[2,'fork',0.1,2],[3,'fork,0.3,1],[3,'exec',0.2,2]] and not [[2,'fork',0.1,2],[3,'exec',0.2,2],[3,'fork,0.3,1]].

I tried the following code:

def mysort(x,y):
return cmp(x[0],y[0])

list.sort(mysort)

This somehow seems to work for a small subset of my input...but not for my real input which has more than 2000 sub-lists(and so obviously i cant trace each pass..). Can someone tell me what i'm missing?
k@t is offline   Reply With Quote