xxxxxxxxxx
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)
xxxxxxxxxx
# sort() will change the original list into a sorted list
vowels = ['e', 'a', 'u', 'o', 'i']
vowels.sort()
# Output:
# ['a', 'e', 'i', 'o', 'u']
# sorted() will sort the list and return it while keeping the original
sortedVowels = sorted(vowels)
# Output:
# ['a', 'e', 'i', 'o', 'u']
xxxxxxxxxx
my_list1 =[("d",4),("a",1),("e",5),("b",2),("c",3)]
my_list2 =[["d",4],["a",1],["e",5],["b",2],["c",3]]
# The 'sort' method will produce similar results
# for both list of lists and list of tuples
# Ascending pair based on second element: 1,2,3,4,5
my_list1.sort(reverse = False, key = lambda element: element[1])
# Descending pair based on second element: 5,4,3,2,1
my_list2.sort(reverse = True, key = lambda element: element[1])
xxxxxxxxxx
l=[1,3,2,5]
l= sorted(l)
print(l)
#output=[1, 2, 3, 5]
#or reverse the order:
l=[1,3,2,5]
l= sorted(l,reverse=True)
print(l)
#output=[5, 3, 2, 1]
xxxxxxxxxx
# example list, product name and prices
price_data = [['product 1', 320.0],
['product 2', 4387.0],
['product 3', 2491.0]]
# sort by price
print(sorted(price_data, key=lambda price: price[1]))
xxxxxxxxxx
l = [64, 25, 12, 22, 11, 1,2,44,3,122, 23, 34]
for i in range(len(l)):
for j in range(i + 1, len(l)):
if l[i] > l[j]:
l[i], l[j] = l[j], l[i]
print l