I have the following lists of pairs:
test1 = [["A", 1],["A", 4], ["D", "Yes"], ["G", 12], ["B", True], ["H", "No"], ["G", 54], ["Z", 412.41]]
test2 = [["D", "Yes"], ["B", True], ["Z", 412.41], ["A", 4], ["G", 12], ["A", 1], ["G", 54], ["H", "No"]]
I want to be able to sort each list into a deterministic order. I don't care about the precise order, but if I sort two identical lists, then I want them to end up in the same order.
I tried:
test1.sort(key=lambda x: x[1])
test2.sort(key=lambda x: x[1])
But I got the error:
TypeError: '<' not supported between instances of 'str' and 'int'
How can I consistently sort a list that contains elements of incompatible types, such as str, bool, int, float?