4

I have a date field in my dictionary Mon Apr 25 13:32:00 PDT 2013 I want to order my list of items by date field.

dictionarylist.sort(key=operator(fieldposition) is not working. Any help?

I want to keep the time field too?

Example data (from comments):

{
    '11163722404385': [
        ('#3', 'Biology', 'A', 'Mon Apr 22 13:32:00 PDT 2013'),
        ('#3', '2089','Irdu', 'B', 'Wed Apr 10 15:31:00 PDT 2013')
    ], 
    '1116372240485': [
        ('#3', '2089','Biology', 'C', 'Mon Apr 25 13:32:00 PDT 2013'),
        ('#3', '2089', 'Math', 'A', 'Mon Apr 22 13:31:14 PDT 2013'),
        ('#3', '2089', 'Literature','C','Mon Apr 22 13:31:00 PDT 2013')
    ]
}
aorcsik
  • 15,271
  • 5
  • 39
  • 49
Think
  • 261
  • 6
  • 13
  • 1
    Can you please give more details? What is "dictionarylist"? A List OF...? – Markon May 22 '13 at 20:33
  • Maybe a list of dictionaries, each with a date field. – aorcsik May 22 '13 at 20:35
  • defaultdict(, {'11163722404385': [('#3', 'Biology', 'A', 'Mon Apr 22 13:32:00 PDT 2013'), ('#3', '2089','Irdu', 'B', 'Wed Apr 10 15:31:00 PDT 2013')], '1116372240485': [('#3', '2089','Biology', 'C', 'Mon Apr 25 13:32:00 PDT 2013'), ('#3', '2089', 'Math', 'A', 'Mon Apr 22 13:31:14 PDT 2013'), ('#3', '2089', 'Literature','C','Mon Apr 22 13:31:00 PDT 2013')]}) Is my dictionarylist – Think May 22 '13 at 20:35
  • 2
    your date isn't a date, its a string – cmd May 22 '13 at 20:36
  • @cmd i am trying to convert to date format. Now sure what format I have to use in datetime.strptime(datestring,format). – Think May 22 '13 at 20:38
  • So, you basically want to sort the sublists according to the "date" field inside them, right? – Markon May 22 '13 at 20:38
  • @Markon that is correct ? Date a string field. – Think May 22 '13 at 20:41
  • 3
    `datetime.datetime.strptime('Mon Apr 22 13:32:00 PDT 2013', '%a %b %d %X PDT %Y')` – cmd May 22 '13 at 20:50
  • @cmd thank you TimeZone could be anything. I can be MST too. How can I add format for timezone ? – Think May 22 '13 at 20:53
  • are you wanting to normalize your time to a specific timezone? – cmd May 22 '13 at 20:55
  • `%Z` is the time zone name – aorcsik May 22 '13 at 20:57
  • @aorcsik I tried %Z it did not work. – Think May 22 '13 at 20:58
  • 1
    not to derail the question, but where are you getting this date. it looks like something out of a database or something. If it is, you can specify better output from the database in your query. – cmd May 22 '13 at 21:01
  • we are getting the data from a client. can i specify more than one time zone in the format section like PST|PDT|MST|EST – Think May 22 '13 at 21:02
  • @cmd since time zone is not that important to me. I am replacing if from the date string field and perform formatRow = row[9].replace('PDT ','') formatRow = formatRow.replace('PST ','') datetime.strptime(formatRow, '%a %b %d %X PDT %Y'). This works for time being. Thank you very much – Think May 22 '13 at 21:40
  • @Think Tell the client they are lame and should use ISO formatted dates! (kidding) (well only half kidding) but I would probably go with `dateutil` like turtle was saying – cmd May 22 '13 at 21:48
  • I have answered your question here: http://stackoverflow.com/a/16675681/1338158 . The only thing you need to change, is the tuple returned by the key function. – Maciej Gol May 22 '13 at 21:58

2 Answers2

2

You should first convert your date strings to python datetime objects, see datetime.strptime for converting a string to a datetime object.

One thing to keep in mind is that you're going to run into issues with the time zone (unless you convert it to a GMT offset). As a work around I recommend using the dateutil module instead of datetime. The advantage being that dateutil is easier to use and still returns datetime objects.

from dateutil import parser
dst_string = 'Mon Apr 25 13:32:00 PDT 2013'
date_obj = parser.parse(dst_str)
print date_obj
>>> datetime.datetime(2013, 4, 25, 13, 32)

You can then sort on the date of the datetime objects.

sorted(d['11163722404385'], key=lambda x: x[-1].date())

EDIT: As @Mark Ransom pointed out time zones can be particularly tricky. Generally you will have to handle abbreviations (PDT, EST, ...) yourself since time zone naming isn't well defined. You can find out more about handling abbreviated time zones with dateutil here.

Community
  • 1
  • 1
FastTurtle
  • 2,301
  • 19
  • 19
  • Be sure to read the documentation for [`parse()`](http://labix.org/python-dateutil#head-c0e81a473b647dfa787dc11e8c69557ec2c3ecd2) particularly the `tzinfos` parameter. – Mark Ransom May 22 '13 at 21:28
1

You can sort your list with a key functions (link), which function should convert the date strings to time with strptime (link).

import time

def to_time(tup):
    return time.strptime(tup[4], "%a %b %d %X PDT %Y")

sorted(defaultdict['1116372240485'], key=to_time)

This will only sort each of your dictionary entries, not the whole dictionary.

aorcsik
  • 15,271
  • 5
  • 39
  • 49
  • thank you for the answer 'cmd' gave answer first. Still +1 for you. But I want time zone %Z is not working. Any idea ? – Think May 22 '13 at 21:02