Sorting dictionary by datetime value

Discussion in 'Python' started by Igor Korot, Feb 8, 2014.

  1. Igor Korot

    Igor Korot Guest

    Hi, ALL,
    I'm trying to do a very easy task: sort python dictionary by value
    where value is a datetime object.

    When trying to do that in Python shell everthing works as expected.

    C:\Documents and Settings\Igor.FORDANWORK>python
    Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit
    (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a = {}
    >>> import datetime
    >>> a['1'] = datetime.datetime(2012,12,28,12,15,30,100)
    >>> a['2'] = datetime.datetime(2012,12,28,12,17,29,100)
    >>> a['3'] = datetime.datetime(2012,12,28,12,16,44,100)
    >>> sorted(a.items(), key=a.get)

    [('1', datetime.datetime(2012, 12, 28, 12, 15, 30, 100)), ('3', datetime.datetim
    e(2012, 12, 28, 12, 16, 44, 100)), ('2', datetime.datetime(2012, 12, 28, 12, 17,
    29, 100))]
    >>>


    However, trying to do the same thing from the script does not sort the
    dictionary:

    sorted(my_dict.items(), key=my_dict.get, reverse=False)
    for key, value in my_dict.items():
    print value, key

    the dictionary prints with unsorted items.

    I am trying to run it on Windows XP and the data are coming from the DB.

    What am I missing?

    Thank you.
    Igor Korot, Feb 8, 2014
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    704
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    532
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    386
    John Machin
    Jul 28, 2006
  4. Martin
    Replies:
    0
    Views:
    333
    Martin
    Dec 27, 2008
  5. Chris Angelico

    Re: Sorting dictionary by datetime value

    Chris Angelico, Feb 8, 2014, in forum: Python
    Replies:
    0
    Views:
    58
    Chris Angelico
    Feb 8, 2014
Loading...

Share This Page