如何使用Python中的for和while循环按字母顺序排序?

sta*_*ack -2 python for-loop while-loop

我还在努力学习Python。我创建了一个水果列表,如下

fruitList = ["pear","apple","strawberry","banana","orange"]
Run Code Online (Sandbox Code Playgroud)

我想使用 for 循环和 while 循环按字母顺序打印它们。

   fruitList.sort()
Run Code Online (Sandbox Code Playgroud)

因为 python 有一个名为 sort() 的函数,因此我可以排序。但是如果我不想使用排序功能怎么办?

但是,现在我有一个问题,如何使用 while 循环进行排序?有人可以告诉我我该怎么做吗?这是我尝试过的,使用 len()

for fruit in fruitList:
    while (len(fruitList[0]) > len(fruit[0])):
            fruit += 1
            print fruit
            continue
Run Code Online (Sandbox Code Playgroud)

当我运行这个程序时没有打印任何内容。在 while 循环中,我是否应该与索引[0] 进行比较,或者我也可以使用排序函数吗?

Ale*_*nov 5

你可以这样做:

for f in sorted(fruitList):
    print(f)
Run Code Online (Sandbox Code Playgroud)

并与同时:

while fruitList:
    print(min(fruitList))
    fruitList.remove(min(fruitList))
Run Code Online (Sandbox Code Playgroud)

但我的意思是,使用“while”是个坏主意

PS sort() 不是一个函数,它是一个列表方法