0

I want to sort the following list of list

[[2, -10], [9, 10], [3, -15], [7, 15], [5, -12], [12, 12], [15, -10], [20, 10], [19, -8], [24, 8]]

I want to sort based on the first element of the list. If the first element is the same, then sort based on the second element in the list

How would I do this in Python 3

In Java, it looks like this

Collections.sort(heights, (a, b) -> (a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]));
swalga
  • 11
  • 3

1 Answers1

0
list.sort(key=lambda x:(x[0], x[1]))
wapean
  • 26
  • 2
  • Hi wapean, welcome to Stack Overflow. This answer was in the low-quality review queue. Code-only answers may solve the question but they are much more useful if you explain why they are correct. The community benefits from theory as well as code to understand your answer fully. – Dan Mar 30 '20 at 12:32