I have a class of this type
class Challenge():
difficulty = Field(type=float)
category = Field(type=str)
and I have a list of Challenge objects that I want to sort in a custom way: I want to order by difficulty with a custom order, and then per each difficulty I want to sort the objects by category, with a different custom order per each difficulty.
I already have a dict with as keys the ordered difficulties, and for each difficulty the ordered list of categories. I need to apply this ordering to my list and I don't know how to apply those criteria to sorting.
I reached this point:
found_challenges.sort(key=lambda x: (x.difficulty, x.category))
Obviously this doesn't sort by the way I want to sort. How can I apply those custom criteria to list sorting?
Example:
ch_1 = Challenge(difficulty=1.0, category='one')
ch_2 = Challenge(difficulty=1.0, category='two')
ch_3 = Challenge(difficulty=2.0, category='one')
ch_4 = Challenge(difficulty=2.0, category='two')
and the ordering dictionary is
{
2.0: ['one', 'two'],
1.0: ['two', 'one']
}
so the ordering should be:
[ch_3, ch_4, ch_2, ch_1]