Sorting a list of dictionaries by dictionary key

N

Nico Grubert

Hi there,

I am looking for a way to sort a list containing dictionaries.

This is my example list:
[{'Title': 'ABC', 'from_datetime': DateTime('2006/04/25 12:45:00
GMT+2')}, {'Title': 'DEF', 'from_datetime': DateTime('2006/04/18
12:45:00 GMT+2')}, {'Title': 'GHI', 'from_datetime':
DateTime('2006/03/10 12:45:00 GMT+2')}]

I want to sort the list by dictionary's key 'from_datetime' so the
sorted list should be:

[{'Title': 'GHI', 'from_datetime': DateTime('2006/03/10 12:45:00
GMT+2')}, {'Title': 'DEF', 'from_datetime': DateTime('2006/04/18
12:45:00 GMT+2')}, {'Title': 'ABC', 'from_datetime':
DateTime('2006/04/25 12:45:00 GMT+2')}]

Any idea how I can sort this list?


Thank you very much in advance,
Nico
 
B

bruno at modulix

Nico said:
Hi there,

I am looking for a way to sort a list containing dictionaries.

This is my example list:
[{'Title': 'ABC', 'from_datetime': DateTime('2006/04/25 12:45:00
GMT+2')}, {'Title': 'DEF', 'from_datetime': DateTime('2006/04/18
12:45:00 GMT+2')}, {'Title': 'GHI', 'from_datetime':
DateTime('2006/03/10 12:45:00 GMT+2')}]

I want to sort the list by dictionary's key 'from_datetime' so the
sorted list should be:

[{'Title': 'GHI', 'from_datetime': DateTime('2006/03/10 12:45:00
GMT+2')}, {'Title': 'DEF', 'from_datetime': DateTime('2006/04/18
12:45:00 GMT+2')}, {'Title': 'ABC', 'from_datetime':
DateTime('2006/04/25 12:45:00 GMT+2')}]

Any idea how I can sort this list?

The common idiom for this is called decorate-sort-undecorate.

unsorted_list = [
{'Title': 'ABC',
'from_datetime': DateTime('2006/04/25 12:45:00 GMT+2')},
{'Title': 'DEF',
'from_datetime': DateTime('2006/04/18 12:45:00 GMT+2')},
{'Title': 'GHI',
'from_datetime': DateTime('2006/03/10 12:45:00 GMT+2')}]

decorated_list = [(d['from_datetime'], d) for d in unsorted_list]
decorated_list.sort()
sorted_list = [t[1] for t in decorated_list]

HTH
 
E

Eric Deveaud

Nico said:
Hi there,

I am looking for a way to sort a list containing dictionaries.

This is my example list:
[{'Title': 'ABC', 'from_datetime': DateTime('2006/04/25 12:45:00
GMT+2')}, {'Title': 'DEF', 'from_datetime': DateTime('2006/04/18
12:45:00 GMT+2')}, {'Title': 'GHI', 'from_datetime':
DateTime('2006/03/10 12:45:00 GMT+2')}]

I want to sort the list by dictionary's key 'from_datetime' so the
sorted list should be:

[{'Title': 'GHI', 'from_datetime': DateTime('2006/03/10 12:45:00
GMT+2')}, {'Title': 'DEF', 'from_datetime': DateTime('2006/04/18
12:45:00 GMT+2')}, {'Title': 'ABC', 'from_datetime':
DateTime('2006/04/25 12:45:00 GMT+2')}]

Any idea how I can sort this list?

sort can take a comparaison function. which specification are
takes 2 elements
returns a negative value if elem1 is lesser than elem2
returns a positive value if elem1 is grater than elem2
returns zero if elem1 == elem2

you may write such fuction

def dictionary_datetime_sorter(d1, d2):
date1 = d1['from_datetime']
date2 = d2['from_datetime']
if date1 < date2: return -1
elif date1 > date2: return 1
else: return 0

you'll have to find out how to compare 2 DateTime

and then, in order to sort your list
your_dictionary_list.sort(dictionary_datetime_sorter)


Eric
 
E

Eric Deveaud

bruno said:
Eric Deveaud wrote:
(snip)

The problem with it is that it may slow down things a lot...

point taken.

I have to purge my mind from the other programing languages I practice. ;-)

Eric
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top