7

In the main dictionary, the date was a value. There were data with same date. So then I took the date and grouped it by creating a 2 dimensional dict where date is the key.

data = {
    "01-01-2015" : "some data",
    "05-05-2015" : "some data",
    "03-04-2015" : "some data"
}

Now I want to sort it by the date descending so that it will look like this:

data = {
    "05-05-2015" : "some data",
    "03-04-2015" : "some data",
    "01-01-2015" : "some data"
}

How can I achieve that?
I am using python 2.6

Imrul.H
  • 5,760
  • 14
  • 55
  • 88
  • 1
    why is `"03-04-2015"` before `"05-05-2015"`? – styvane Dec 07 '15 at 08:41
  • *By date key?* The second code block is your expect or the first one is? And by the way, you need [OrderedDict](https://docs.python.org/3/library/collections.html#collections.OrderedDict). – Remi Guan Dec 07 '15 at 08:42
  • 3
    A simple google/stackoverflow search will tell you clearly that **a python dictionary can't be sorted.** A different structure will have to be used. (ex- OrderedDict). Try it and let us know. – Shivendra Dec 07 '15 at 08:43
  • Possible duplicate of [How can I sort a dictionary by key?](http://stackoverflow.com/questions/9001509/how-can-i-sort-a-dictionary-by-key) – Muhammad Tahir Dec 07 '15 at 08:43
  • I have edited the question. – Imrul.H Dec 07 '15 at 13:00

2 Answers2

16

The idea here is to use OrderedDict as dictionary has no scope for ordering while OrderedDict preserves the entry order.

The idea here is to use list of tuples of (key, value) pairs as dictionary has no scope for ordering while OrderedDict preserves the entry order. Also we must convert the keys to actual datetime object for the sort to work perfectly otherwise sorting on string based fields happen alphabetically.

Code :

from datetime import datetime
#from collections import OrderedDict


data = {
    "01-01-2015" : "some data",
    "05-05-2015" : "some data",
    "03-04-2015" : "some data"
}

#ordered_data = OrderedDict(
#    sorted(data.items(), key = lambda x:datetime.strptime(x[0], '%d-%m-%Y'), reverse=True))

#Since OP is using python 2.6 where OrderedDict is not present, I changed the solution and the original solution is commented out
ordered_data = sorted(data.items(), key = lambda x:datetime.strptime(x[0], '%d-%m-%Y'), reverse=True)

print(ordered_data)

Output :

[('05-05-2015', 'some data'), ('03-04-2015', 'some data'), 
('01-01-2015', 'some data')]

Explanation : (For converting keys to datetime for sorting)

If you keep the date field as string then the lexicographical order is considered and 3 comes after 0 hence a>b is True but same is not true for their date counterparts. Feb definitely comes after Jan.

a = "30-01-2015"
b = "01-02-2015"

print(a>b)    #True

a_datetime = datetime.strptime(a, '%d-%m-%Y')
b_datetime = datetime.strptime(b, '%d-%m-%Y')

print(a_datetime>b_datetime) #False
Shivendra
  • 1,076
  • 2
  • 12
  • 26
  • Sorry but I forgot to mention the python version. It's 2.6 that I am using and OrderedDict is from 2.7+ – Imrul.H Dec 07 '15 at 13:01
  • 1
    Just use `ordered_data = sorted(data.items(), key = lambda x:datetime.strptime(x[0], '%d-%m-%Y'), reverse=True)`. Only thing is you will not be able to use dictionary's `key` advantage as you will get a list of tuple as output. – Shivendra Dec 07 '15 at 14:00
7

You can use the sorted method of python that will automatically sort your data on date and return a list.

data = {"01-01-2015" : "some data","05-05-2015" : "some data","03-04-2015" : "some data"}
sorted(data.items(), reverse=True)

that will produce the result:

[('05-05-2015', 'some data'), ('03-04-2015', 'some data'), ('01-01-2015', 'some data')]
Aalok kumar
  • 309
  • 2
  • 5