# You can use a lambda function, but you'll have to specify the index of the sorting key.
A =[[100,'Yes'],[40,'Maybe'],[60,'No']]print("Sorted List A based on index 0: % s"%(sorted(A, key=lambda x:x[0])))
B =[[2,'Dog'],[0,'Bird'],[7,'Cat']]print("Sorted List A based on index 1: % s"%(sorted(B, key=lambda x:x[1])))# Also, you can use .sort() if you want to sort just by the first index
A =[[55,90],[45,89],[90,70]]
A.sort()print("New sorted list A is % s"%(A))
A.sort(reverse=True)print("New reverse sorted list A is % s"%(A))# You can even change the key sort, if you want to sort by length for example:
A =[[5,90,'Hi',66],[80,99],[56,32,80]]
A.sort(key=len)# <-print("New sorted list A is % s"%(A))
from operator import itemgetter
A =[[10,8],[90,2],[45,6]]print("Sorted List A based on index 0: % s"%(sorted(A, key=itemgetter(0))))
B =[[50,'Yes'],[20,'No'],[100,'Maybe']]print("Sorted List B based on index 1: % s"%(sorted(B, key=itemgetter(1))))"""
Output:
Sorted List A based on index 0: [[10, 8], [45, 6], [90, 2]]
Sorted List B based on index 1: [[100, 'Maybe'], [20, 'No'], [50, 'Yes']]
"""
# 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']
from operator import itemgetter
A =[[10,8],[90,2],[45,6]]print("Sorted List A based on index 0: % s"%(sorted(A, key=itemgetter(0))))
B =[[50,'Yes'],[20,'No'],[100,'Maybe']]print("Sorted List B based on index 1: % s"%(sorted(B, key=itemgetter(1))))
# example list, product name and prices
price_data =[['product 1',320.0],['product 2',4387.0],['product 3',2491.0]]# sort by priceprint(sorted(price_data, key=lambda price: price[1]))
>>> L =['abc','ABD','aBe']>>>sorted(L, key=str.lower, reverse=True)# Sorting built-in['aBe','ABD','abc']>>> L =['abc','ABD','aBe']>>>sorted([x.lower()for x in L], reverse=True)['abe','abd','abc']
"""Sort in ascending and descending order"""
list_test =[2,1,5,3,4]#ascending is by default for sort#Time Complexity: O(nlogn)
list_test.sort()#For descending order#Time Complexity: O(nlogn)
list_test.sort(reverse=True)#For user-define order
list_test.sort(key=..., reverse=...)
data_list =[-5,-23,5,0,23,-6,23,67]
new_list =[]while data_list:
minimum = data_list[0]# arbitrary number in list for x in data_list:if x < minimum:
minimum = x
new_list.append(minimum)
data_list.remove(minimum)print new_list
# Sort with an inner object# Here it will sort with "book_no"# [# {# "key": "book-key-1",# "book_no": 1,# "name": "My Book Name 1"# },# {# "key": "book-key-2",# "book_no": 2,# "name": "My Book Name 2"# }# ]defsortOnNumber(e):return e['book_no']@app.get('/getBooks')defgetBooks():
res =next(booksDb.fetch())
res.sort(key=sortOnNumber)if res:return res
raise HTTPException(404,"Not found")
stuff_1 =[3,4,5,2,1]
stuff_1.sort()print(stuff_1)# Output: [1, 2, 3, 4, 5]
stuff_2 =['bob','john','ann']
stuff_2.sort()print(stuff_2)# Output: ['ann', 'bob', 'john']
stuff_4 =['book',89,5.3,True,[1,2,3],(4,3,2),{'dic':1}]# print(stuff_4.sort())# This will give us an error# TypeError: '<' not supported between instances of 'int' and 'str'