-4

Let's say I have a list:

b = [ {'id':'e1'},{'id':'e100'},{'id':'e3'},{'id':'e2'}  ]

I want to sort it by id numbers to look like that:

b = [{'id': 'e1'}, {'id': 'e2'}, {'id': 'e3'}, {'id': 'e100'}]
Tomas
  • 13
  • 2
  • 4
    No, or you're not sure? Could you become sure, *then* try to write the question? – jonrsharpe Feb 23 '16 at 12:10
  • Possible duplicate of [Sort a Python dictionary by value](http://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value) – SiHa Feb 23 '16 at 12:38
  • 1
    True duplicate: [Sort a list of dicts by dict values](http://stackoverflow.com/questions/2878084/sort-a-list-of-dicts-by-dict-values) – gil Feb 23 '16 at 12:58

1 Answers1

2

You could use sorted. The following solution works, provided the value of 'id' starts with a single character followed by numbers.

sorted(b, key=lambda x: int(x['id'][1:]))
JRodDynamite
  • 12,325
  • 5
  • 43
  • 63