#πŸ”’ How to bubble sort words in a list?

92 messages Β· Page 1 of 1 (latest)

frozen orbit
#

Im trying to bubble sort this list but no matteer how i try, i still cant get it?
list = ['Banana', 'Apple, 'Pineapple', Mango']

covert thicketBOT
#

@frozen orbit

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.

whole quarry
frozen orbit
#

waiittt

#

this is the correct codes right?

whole quarry
frozen orbit
#

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?

whole quarry
#

Okay, following so far

frozen orbit
#

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?

whole quarry
#

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

frozen orbit
#

okok

whole quarry
#

Is there a reason you're using a custom written bubble sort instead of sorted() or list.sort()?

frozen orbit
#

homework lemon_angrysad

#

i wld use sorted() if i can tbh

frozen orbit
whole quarry
#

Okay, yeah, I see people writing their own sort algorithms for homework all the time, just not usually part of a larger project

whole quarry
# frozen orbit is there another way to do this without this magic method?

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.

frozen orbit
#

this is my custojm version....

#

its nt working.....

#

lemon_angrysad pls helppp

whole quarry
#

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.

frozen orbit
#

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

whole quarry
frozen orbit
#

i thot > or < only works on int type

#

OMG

#

it workss

whole quarry
frozen orbit
#

OMGGG

#

thank you soo muchh

#

i hv been stressing this for like 3hours

#

omggg

#

thank you thank you

whole quarry
#

I'm not sure I like that else: break in there, you sure its right?

frozen orbit
#

....

#

oh

#

ya

#

thts for the prev code

#

lemme remove it

#

shld it be else: pass?

whole quarry
frozen orbit
#

oh

#

does it just ignore if x !> y?

whole quarry
#

Else pass does the same thing as just not having an else

whole quarry
frozen orbit
#

ye

#

but hey

#

how do u do that

#

special code thingy

#

the j

whole quarry
#

`j`

frozen orbit
#

'j'

whole quarry
#

backticks

#

by your ~

frozen orbit
#

j

#

OHHH

#

dam

#

btw are you paid to help us?

#

or is it an interests

#

lemon_sweat itsok

#

i was jst curious why ure so helpful

#

thanks again for helping me

#

/close

#

!close

covert thicketBOT
#
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.