Why are the results of the two implementations different?

>>> students=[("john","A",15),("jane","B",12),("dave","B","10")]
>>> sorted(students,key=lambda s:s[2])
[("jane", "B", 12), ("john", "A", 15), ("dave", "B", "10")]
>>> sorted(students,key=lambda s:s[2])
[("jane", "B", 12), ("john", "A", 15), ("dave", "B", "10")]
>>> sorted(students,key=lambda s:s[2],reverse=True)
[("dave", "B", "10"), ("john", "A", 15), ("jane", "B", 12)]
>>> sorted(students, key=lambda s: s[2], reverse=True)
[("dave", "B", "10"), ("john", "A", 15), ("jane", "B", 12)]


>>> students = [("john", "A", 15), ("jane", "B", 12), ("dave", "B", 10)]
>>> sorted(students, key=lambda s: s[2])
[("dave", "B", 10), ("jane", "B", 12), ("john", "A", 15)]
>>> sorted(students, key=lambda s: s[2], reverse=True)
[("john", "A", 15), ("jane", "B", 12), ("dave", "B", 10)]-sharp-sharp-sharp 

sources of topics and their own ideas

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?

Mar.26,2021
The third element in the

tuple, one is by int and the other is by str type, and your key is sorted by the third element. Int and str cannot be compared in size.


python3.7 runs the error report directly. The elements are not exactly the same in


. One is '10' at the end, and the other is 10

.
Menu