Skip to content
Advertisement

How to reorder a python OrderedDict using cascading list values

I’ve seen simpler examples of this but I’m trying to get a bit more complex and can’t work it out.

If I have an OrderedDict in which the key describes a person and the value is a list of their [age, gender, hairColor].

JavaScript

What I want is to sort the OrderedDict first by age, then by gender, and then by hairColor. This would then be the correct result I’m looking for.

JavaScript

I have figured out a way to sort by age.

JavaScript

However, I’ve tried multiple ways to extrapolate this approach to then re-sort any tie-breakers that are the same age and then again any tie-breakers that are the same gender, but it’s getting really complex and I keep thinking there must be a simpler way.

I’m hoping someone out there has an idea to help me out. Thank you all in advance.

Advertisement

Answer

The default sort order is already lexicographic.

JavaScript

Therefore:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement