0

I need to sort a list of lists, where each entry in the outer list is a list of three integers like this:

[[3,1,0],[1,2,3],[3,2,0],[3,1,1]]

The trick is that I need to sort it by entry[0], and if there's a tie, then sort them by entry[1], and if that's also a tie, sort by entry[2], so the list above should be:

[[3,2,0],[3,1,1],[3,1,0],[1,2,3]]

I keep overthinking my loops and sort statements and I haven't been able to get a way to check the entries in order without rearranging my lists by those values instead of keeping the hierarchy.

Any suggestions?

1 Answer 1

8

It's a default behaviour for comparing lists.

x = [[3,1,0],[1,2,3],[3,2,0],[3,1,1]]
x.sort(reverse = True)
print(x)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.