Sort Descending
The .sort() method also accepts some optional parameters. This is the .sort() function signature:
- The
keyparameter allows us to customize the sorting order. We will learn more about this soon. - The
reverseparameter is a boolean value that determines whether the list should be sorted in descending order. By default, it is set toFalse.
If we want to sort a list in descending order, we can set the reverse parameter to True.
We can actually omit the key parameter and only pass the reverse parameter, by using a feature of Python called keyword arguments.
It's also possible for us to sort the list in ascending order and then manually reverse the result.
Challenge
Implement the following functions:
sort_words(words: List[str]) -> List[str]- This function accepts a list of words and returns the list of words sorted in descending order.sort_numbers(numbers: List[int]) -> List[int]- This function accepts a list of numbers and returns the list of numbers sorted in descending order.sort_decimals(numbers: List[float]) -> List[float]- This function accepts a list of decimal numbers and returns the list of decimal numbers sorted in descending order.