Sub-sort after sort

KeefTM keeftm at gmail.com
Fri Nov 2 23:06:50 CET 2007


On Nov 2, 2:45 pm, kee... at gmail.com wrote:
> Hello, I have been sorting a list of dicts using the following
> function:
>
> result_rs = sorted(unsort_rs, key=itemgetter(orderby))
>
> and this works fine. Now I am looking to perform a subsort as well.
> For example, I have this:
>
> test = [{'name': 'John Smith', 'location': 'CA',},{'name': 'John
> Smith', 'location': 'AZ',},]
>
> I would want to sort by name first, then sub sort by location. Any
> ideas? Thanks!

Well, I found a way to do it. Just create a new key and value that is
a combination of two other values, like this:

test = [{'name': 'John Smith', 'location': 'AZ','sort':'John SmithAZ'},
{'name': 'John
> Smith', 'location': 'CA', 'sort':'John SmithCA'},]

Not pretty in the slightest, but it works.




More information about the Python-list mailing list