2

I am trying to sort a dictionary and I have looked at numerous examples but they all show very simple dictionaries. I have looked at orderedDict, itemgetter, lambda etc. but I cannot see how to sort this dictionary either by position, homepoints or awaypoints:

{
    u'SwanseaCityFC': {
        'position': 12,
        'awayPoints': 7,
        'homePoints': 8
    },
    u'TottenhamHotspurFC': {
        'position': 6,
        'awayPoints': 11,
        'homePoints': 9
    },
    u'StokeCityFC': {
        'position': 14,
        'awayPoints': 9,
        'homePoints': 4
    },
    u'AstonVillaFC': {
        'position': 20,
        'awayPoints': 4,
        'homePoints': 1
    },
    u'ManchesterCityFC': {
        'position': 1,
        'awayPoints': 10,
        'homePoints': 12
    },
    u'AFCBournemouth': {
        'position': 17,
        'awayPoints': 5,
        'homePoints': 5
    },
    u'CrystalPalaceFC': {
        'position': 7,
        'awayPoints': 9,
        'homePoints': 6
    },
    u'NewcastleUnitedFC': {
        'position': 19,
        'awayPoints': 3,
        'homePoints': 5
    },
    u'NorwichCityFC': {
        'position': 16,
        'awayPoints': 6,
        'homePoints': 4
    },
    u'WatfordFC': {
        'position': 13,
        'awayPoints': 10,
        'homePoints': 5
    },
    u'ManchesterUnitedFC': {
        'position': 4,
        'awayPoints': 11,
        'homePoints': 11
    },
    u'LeicesterCityFC': {
        'position': 5,
        'awayPoints': 10,
        'homePoints': 10
    },
    u'ChelseaFC': {
        'position': 15,
        'awayPoints': 5,
        'homePoints': 7
    },
    u'WestHamUnitedFC': {
        'position': 3,
        'awayPoints': 14,
        'homePoints': 7
    },
    u'SunderlandAFC': {
        'position': 18,
        'awayPoints': 3,
        'homePoints': 5
    },
    u'SouthamptonFC': {
        'position': 8,
        'awayPoints': 8,
        'homePoints': 7
    },
    u'EvertonFC': {
        'position': 11,
        'awayPoints': 10,
        'homePoints': 5
    },
    u'LiverpoolFC': {
        'position': 9,
        'awayPoints': 8,
        'homePoints': 8
    },
    u'ArsenalFC': {
        'position': 2,
        'awayPoints': 13,
        'homePoints': 10
    },
    u'WestBromwichAlbionFC': {
        'position': 10,
        'awayPoints': 11,
        'homePoints': 4
    }
}

Any help appreciated!

garyk1968
  • 121
  • 3
  • 8
  • 1
    `dict`s do not retain any sort, so you need to build a list or use an OrderedDict just to maintain the order of elements. Putting the elements into order requires a key function. I think there are some great examples here: https://wiki.python.org/moin/HowTo/Sorting – RobertB Oct 29 '15 at 16:46
  • [This](http://stackoverflow.com/questions/9001509/how-can-i-sort-a-dictionary-by-key) does not solve your problem?! – Cleb Oct 29 '15 at 16:47
  • The Python dict data structure has no inherent order. Perhaps you should consider a dataframe (Pandas) type structure. – jgloves Oct 29 '15 at 16:47

2 Answers2

2

Using Sort dictionary by multiple values as a starting point, here's an example of what you can do for sorting.

dict = { ... your dictionary ... }
# sort by position
for item in sorted(dict, key = lambda k: dict[k]['position']):
    print(dict[item])

# sort by awayPoints, then position; note the lambda uses a tuple
for item in sorted(dict, key = lambda k: (dict[k]['awayPoints'], dict[k]['position'])):
    print(mylist[item])
0

Go to the collections documentation and you can see the OrderedDict implementation notes https://docs.python.org/3/library/collections.html#collections.OrderedDict

There is an example like below:

>>> # dictionary sorted by value
>>> OrderedDict(sorted(d.items(), key=lambda t: t[1]))
OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])

which you can then extend to your own usage:

>>> d = {'banana':{'key':3},'apple':{'key':4}, 'pear': {'key':1}, 'orange': {'key':2}}
>>> OrderedDict(sorted(d.items(), key=lambda t: t[1]['key']))
OrderedDict([('pear', {'key': 1}), ('orange', {'key': 2}), ('banana', {'key': 3}), ('apple', {'key': 4})])
R Nar
  • 5,465
  • 1
  • 16
  • 32