Sorting a tuple of dicts
python, sorting
Solution
You could do something like:
import operator
...
sortcompanies.sort(key=operator.itemgetter("companyname"))
I think that's a matter of taste.
EDIT I got `companyid` in stead of `companyname`. Corrected that error.
Problem
I am new to Python and am curious if I am doing this correctly. I have a tuple of dicts (from a database call): ``` companies = ( { 'companyid': 1, 'companyname': 'Company C' }, { 'companyid': 2, 'companyname': 'Company A' }, { 'companyid': 3, 'companyname': 'Company B' } ) ``` I want to sort this on companyname. Is there a more correct way than this to do it? ``` sortcompanies = list(companies) sortcompanies.sort(lambda x,y: cmp(x['companyname'],y['companyname'])) ``` Thanks for your criticism!