#π How to bubble sort words in a list?
92 messages Β· Page 1 of 1 (latest)
@frozen orbit
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.
Use the exact same bubble sort code you'd use to sort a bunch of integers. Change nothing. The if a > b: in your bubble sort code will work the same for words putting them in the right order too.
That looks good to me
okok
reall final question
so lets say what if i want to sort words
but
its an object
like the list stores the object
and i need to use the getter method to access the words for sorting?
Okay, following so far
omg
wait
so booklist is the list im storing my objects
the thing is i want to sort the publisher in this object in ascending order
is it still the same as before?
If the objects are a class you made, I would add the magic method __lt__ so that obj1 < obj2 will be a valid statement, and then run it through the same sort algorithm
okok
Is there a reason you're using a custom written bubble sort instead of sorted() or list.sort()?
is there another way to do this without this magic method?
Okay, yeah, I see people writing their own sort algorithms for homework all the time, just not usually part of a larger project
yea
A bunch. If you were allowed to use .sort/sorted then you could use the key= argument, but sounds like that is out. If you made your class a dataclass, you could add an order=, though that just a roundabout way of defining the __lt__ method. Other than that, you may have to make a custom version of your sort algorithm where you change the if a > b line to something that works with your objects.
I'm confused by the .index stuff. It should look exactly like the other bubble sort except the if line should be the only line that needs changing.
OHH
okok wai
i forgot to show another party
part*
this
i tried getting the index of each letter
so tht i can compare
ik its dumb
You can just directly compare the letters if 'A' > 'B':
i didnt know until u said it just now....
i thot > or < only works on int type
OMG
it workss
They work for lots of things, integers, floats, strings, and even lists, tuples
OMGGG
thank you soo muchh
i hv been stressing this for like 3hours
omggg
thank you thank you
I'm not sure I like that else: break in there, you sure its right?
You don't need at else
Else pass does the same thing as just not having an else
Right, it doesn't swap the two elements if the if statement is false and moves onto checking the next j
`j`
'j'
j
OHHH
dam
btw are you paid to help us?
or is it an interests
itsok
i was jst curious why ure so helpful
thanks again for helping me
/close
!close
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.
