Vinu K S Vinu K S - 2 months ago 6x
Python Question

How do I perform secondary sorting in python?

If i have a list of numbers


I want to sort it first by some function
and then for numbers with the same value of
i want it to be sorted by the magnitude of the number.

This code does not seem to be working.

list5 = sorted(list5)
list5 = sorted(list5, key = lambda vertex: degree(vertex))

Secondary sorting first: list5 is sorted based on magnitude.
Primary sorting next: list5 is sorted based on some function of the numbers.


Sort it by a (firstkey, secondkey) tuple:

sorted(list5, key=lambda vertex: (degree(vertex), vertex))