Sort sub-dicts in dict by subdict value

this is my json:

{"data": [{"L": "Leinster"}, {"RN": "Roscommon"}, {"G": "Galway"}, {"LS": "Laois"}, {"LD": "Longford"}, {"OY": "Offaly"}, {"KK": "Kilkenny"}, {"SO": "Sligo"}, {"C": "Connaught"}, {"CO": "Cork"}, {"M": "Munster"}, {"WD": "Waterford"}, {"CE": "Clare"}]}

I want to sort so that the dict is ordered by the "abc" value, for example:

  {"data": [ {"CE": "Clare"},{"C": "Connaught"}, {"CO": "Cork"}, {"G": "Galway"},{"KK": "Kilkenny"}, {"LS": "Laois"},{"L": "Leinster"}, {"LD": "Longford"}, {"M": "Munster"},{"OY": "Offaly"}, {"RN": "Roscommon"}, {"SO": "Sligo"}, {"WD": "Waterford"} ]}

some ideas? tried this Python: compile this dictionary (dict in dict)  but this is sorting the values. not the values ​​in the dict.

thank!

+3
source share
3 answers

For Python 2

>>> d = {'data': [{'L': 'Leinster'}, {'RN': 'Roscommon'}, {'G': 'Galway'}, {'LS': 'Laois'}, {'LD': 'Longford'}, {'OY': 'Offaly'}, {'KK': 'Kilkenny'}, {'SO': 'Sligo'}, {'C': 'Connaught'}, {'CO': 'Cork'}, {'M': 'Munster'}, {'WD': 'Waterford'}, {'CE': 'Clare'}]}
>>> dict((k,sorted(v,key=dict.values)) for k,v in d.iteritems())
{'data': [{'CE': 'Clare'}, {'C': 'Connaught'}, {'CO': 'Cork'}, {'G': 'Galway'}, {'KK': 'Kilkenny'}, {'LS': 'Laois'}, {'L': 'Leinster'}, {'LD': 'Longford'}, {'M': 'Munster'}, {'OY': 'Offaly'}, {'RN': 'Roscommon'}, {'SO': 'Sligo'}, {'WD': 'Waterford'}]}

Here is a python 3 solution. It would be great to know if something was better than this, since I don't like it.

>>> {k:sorted(v,key=lambda x: tuple(x.values())) for k,v in d.items()}
{'data': [{'CE': 'Clare'}, {'C': 'Connaught'}, {'CO': 'Cork'}, {'G': 'Galway'}, {'KK': 'Kilkenny'}, {'LS': 'Laois'}, {'L': 'Leinster'}, {'LD': 'Longford'}, {'M': 'Munster'}, {'OY': 'Offaly'}, {'RN': 'Roscommon'}, {'SO': 'Sligo'}, {'WD': 'Waterford'}]}
+4
source

or maybe you can just do

Updated to sort by value

In [41]: r={"data": [{"L": "Leinster"}, {"RN": "Roscommon"}, {"G": "Galway"}, {"LS": "Laois"}, {"LD": "Longford"}, {"OY": "Offaly"}, {"KK": "Kilkenny"}, {"SO": "Sligo"}, {"C": "Connaught"}, {"CO": "Cork"}, {"M": "Munster"}, {"WD": "Waterford"}, {"CE": "Clare"}]}

In [42]: for v in r.itervalues():
....:     v.sort(key=dict.values)
....: 

In [43]: r
Out[43]:  
{'data': [{'CE': 'Clare'},
      {'C': 'Connaught'},
      {'CO': 'Cork'},
      {'G': 'Galway'},
      {'KK': 'Kilkenny'},
      {'LS': 'Laois'},
      {'L': 'Leinster'},
      {'LD': 'Longford'},
      {'M': 'Munster'},
      {'OY': 'Offaly'},
      {'RN': 'Roscommon'},
      {'SO': 'Sligo'},
      {'WD': 'Waterford'}]}
+1
source

, , !

>>> r={"data": [{"L": "Leinster"}, {"RN": "Roscommon"}, {"G": "Galway"}, {"LS": "Laois"}, {"LD": "Longford"}, {"OY": "Offaly"}, {"KK": "Kilkenny"}, {"SO": "Sligo"}, {"C": "Connaught"}, {"CO": "Cork"}, {"M": "Munster"}, {"WD": "Waterford"}, {"CE": "Clare"}]}
>>>
>>> map(functools.partial(list.sort, key=dict.values), r.itervalues())
[None]
>>>
>>> r
{'data': [{'CE': 'Clare'}, {'C': 'Connaught'}, {'CO': 'Cork'}, {'G': 'Galway'}, {'KK': 'Kilkenny'}, {'LS': 'Laois'}, {'L': 'Leinster'}, {'LD': 'Longford'}, {'M': 'Munster'}, {'OY': 'Offaly'}, {'RN': 'Roscommon'}, {'SO': 'Sligo'}, {'WD': 'Waterford'}]}
+1

All Articles