Assuming you have a list of tuples and the need to sort them based on 2 values of the tuple where one should be descending in value and the other ascending in value as second priority.
For example, the following code will sort based on the priority of the first value first, then afterwards based on the second value but both being descending in value due to the reversed=True
. The priority needs to be remained, but the second value should be ascending.
my_list = [(1, 2, 3), (1, 3, 2), (2, 1, 3)]
my_list.sort(key=lambda tup: (tup[0], tup[1]), reverse=True)
>>> [(2, 1, 3), (1, 3, 2), (1, 2, 3)]
The first element of the tuple is being prioritised and sorted, then afterwards the second element is used to sort around. But both are in descending value. How can the list be sorted so that the tuple is first sorted based on the first value descending and then the second value ascending?
The desired output in this case would be >>> [(2, 1, 3), (1, 2, 3), (1, 3, 2)]