#๐Ÿ”’ Why does the sort function this way?

13 messages ยท Page 1 of 1 (latest)

zenith sail
#

d = {a:[5,0,0],b:[0,2,3],c:[0,3,2]}
voted_names = sorted(d.keys())
test = sorted(voted_names, key= lambda x: d[x], reverse=True))

test ends up being [a,c,b]
how is it handling 3 seperate values in the sort is it just going from front to back?

flat timberBOT
#

@zenith sail

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

quiet elm
#

Yes, it compares the first item to sort. If there is a tie, it uses the second item to tie break. If its a tie, it goes to the 3rd item, etc.

zenith sail
#

wow thats cool, is that documented somewhere I could have found without having to bug you?

quiet elm
zenith sail
#

ah well no worries just wanted to make sure I wasn't being a dummy and rtfm

#

thank you for the answer

low salmon
#

@zenith sail when you're reading that page, you should also read the part about sort stability

#

I meant this page

zenith sail
#

I think I see, the sort I listed above is essentially doing what the stability and complex sort section is saying can be done in multiple steps in one

flat timberBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.