sortList = ['a', '', 'bbb']
sortList.sort()
>>> ['a', 'bbb', 'cc']
sortList = ['a', 'cc', 'bbb']
# "" , :
def sortByAlphabet (inputStr):
return inputStr[0] # ,
# , :
def sortByLength (inputStr):
return len(inputStr) # ,
print u' : ', sortList # >>> ['a', 'cc', 'bbb']
sortList.sort( key=sortByAlphabet ) #
print u' : ', sortList # >>> ['a', 'bbb', 'cc']
sortList.sort( key=sortByLength ) #
print u' : ', sortList # >>> ['a', 'cc', 'bbb']
# , :
sortList.sort( key=sortByLength , reverse=True ) #
print u' , : ', sortList # >>> ['bbb', 'cc', 'a']
newList = sorted(sortList)
newList = sorted(sortList, key=sortByLength)
Source: https://habr.com/ru/post/138535/