Ok, here’s what I’m trying to do… I know that itemgetter() sort could to alphabetical sort easy, but if I have something like this:
[{‘Name’:’TOTAL’, ‘Rank’:100},
{‘Name’:’Woo Company’, ‘Rank’:15},
{‘Name’:’ABC Company’, ‘Rank’:20}]
And I want it sorted alphabetically (by Name) + include the condition that the one with Name:’TOTAL’ should be listed last in the sequence, like this:
[{‘Name’:’ABC Company’, ‘Rank’:20},
{‘Name’:’Woo Company’, ‘Rank’:15},
{‘Name’:’TOTAL’, ‘Rank’:100}]
How would I do that?
The best approach here is to decorate the sort key… Python will sort a tuple by the tuple components in order, so build a tuple key with your sorting criteria:
This results in a sort key of:
Since False sorts earlier than True, the ones whose names aren’t TOTAL will end up together, then be sorted alphabetically, and TOTAL will end up at the end.