How to sort a list of lists by weekday

This is the place for queries that don't fit in any of the other categories.

How to sort a list of lists by weekday

Postby redmage123 » Thu May 09, 2013 2:51 pm

Hello all,

I have a list that looks like this:
Code: Select all
DOW_LIST=['Sun','Mon','Tue','Wed','Thu','Fri','Sat']
data = [['Sun','10:15'],['Sat','11:23'],['Mon','12:25']]

I'd like to sort the data list so that it sorts by weekday and then by time (i.e. by row 0 and then row 1)
the tricky bit is that I don't want to do either a lexical or a numeric sort on the first row, but rather by
the index of the DOW_LIST.

Can someone show me a good way to do this?

Thanks,

-- redmage123
Last edited by joneshf on Sat May 11, 2013 1:36 pm, edited 1 time in total.
Reason: USE CODE TAGS!
redmage123
 
Posts: 2
Joined: Thu May 09, 2013 2:45 pm

Re: How to sort a list of lists by weekday

Postby redmage123 » Thu May 09, 2013 4:23 pm

I figured it out...

Here's an example...

Code: Select all
DOW=['Sun','Mon','Tue','Wed','Thu','Fri','Sat']
data = [['Tue','foo'],['Mon','bar'],['Sun','baz']]

data.sort(key=lambda row: (DOW.index(row[0]))

-- redmage123
Last edited by joneshf on Sat May 11, 2013 1:36 pm, edited 1 time in total.
Reason: USE CODE TAGS!
redmage123
 
Posts: 2
Joined: Thu May 09, 2013 2:45 pm

Re: How to sort a list of lists by weekday

Postby joneshf » Sat May 11, 2013 1:35 pm

viewtopic.php?f=10&t=145

Particularly the part about the code tags.
joneshf
 
Posts: 8
Joined: Thu Feb 07, 2013 3:48 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests