OK, first of all you're trying to sort according to alphabetical, then why are you considering the length of the word on your list instead of their name?
Also, there are some mistakes in your code and I don't understand what you're trying to do.
for fruit in fruitList:
while (len(fruitList[0]) > len(fruit[0])):
fruit += 1
print fruit
continue
First, len(fruitList[0])
is the length of the first item in your list (let's say it's pear
).
In the first iteration, fruit
will be fruitList[0]
, pear
.
When you're doing len(fruitList[0]) > len(fruit[0])
you're doing (in the first iteration) len("pear") > len('p')
which is always 'True' as long as you don't have an empty string ... in which case fruit[0]
would throw an error.
Now, why are you doing this? What are you trying to do?
Anyway, let's move on, since the above condition was 'True', then the next line of code is fruit +=1
but fruit
is a string and '1' is an int, so you're trying to add a 'str' + 'int' which raises an error.
Sooooo.... your code doesn't run, it throws an error, if you're saying that your code doesn't print anything, then you're getting 'False' in your while chunk, so the code you posted isn't the exact same thing you're running in your computer, am I right?
Finnally, to answer your question, to order your list in alphabetical order, you have to check for the letters.
First set the first item in the list as the first item.
Then check the second item in your list and compare with the item before, if the second goes before, then move it.
Then check the next item, compare with the item before it, if it goes before, then compare with the item before that one and move accordingly... repeat until you've check everyitem in the list. ---> Or use Bubble sort as explained above (just read that answer)