Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 22nd, 2005, 2: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
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:02 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC