Definition
The sort() method sorts the list ascending by default. You can also make a function to decide the sorting criteria.
Syntax
list.sort(reverse=True|False, key=myFunc)
Parameters
Parameter | Description |
---|---|
reverse | Optional. reverse=True will sort the list descending. Default is reverse=False |
key | Optional. A function to specify the sorting criteria |
Examples:
cars = ['Nissan', 'Ford', 'Volkswagen']
cars.sort(reverse=True)
print(cars)
# using a function as criteria for sorting
def myFunction(e):
return len(e)
cars.sort(key=myFunction)
print(cars)