Vinu K S - 2 months ago 6x

Python Question

If i have a list of numbers

`[4,2,5,1,3]`

I want to sort it first by some function

`f`

`f`

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.

Answer

Sort it by a (firstkey, secondkey) tuple:

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

Source (Stackoverflow)

Comments