
Now all the numbers are arranged in reverse, with the largest value being on the left hand side and the smallest on the right. Let's reuse the same example from the previous section, but this time make it so the numbers are sorted in reverse order: # a list of numbers The general syntax to do this would look something like this: list_name.sort(reverse=True) To sort list items in descending order, you need to use the optional reverse parameter with the sort() method, and set its value to True.
XSORT PYTHON HOW TO
How to Sort List Items in Descending Order Using the sort() Methodĭescending (or decreasing) order is the opposite of ascending order - elements are arranged from highest to lowest value.

In this case, each string contained in the list was sorted in aplhabetical order.Īs you saw in both examples, the original lists were directly changed. #sort list in-place in alphabetical order You can also achieve the same when working with a list of strings: # a list of strings In the example above, numbers are sorted from smallest to largest. Let's take a look at the following example which shows how to sort a list of whole numbers: # a list of numbers The general syntax to do this would look something similar to the following: list_name.sort() The lowest value is on the left hand side and the highest value is on the right. How to Sort List Items in Ascending Order Using the sort() MethodĪs mentioned earlier, by default, sort() sorts list items in ascending order.Īscending (or increasing) order means that items are arranged from lowest to highest value. The sort() method returns None, which means there is no return value since it just modifies the original list. It takes a function or method which is used to specify any detailed sorting criteria you may have. Setting it to True sorts the list backwards, in desceding order. The default value is False, meaning the list is sorted in ascending order. It takes a Boolean value, meaning the value is either True or False. It specifies whether the list will be sorted in ascending or descending order. reverse is the first optional parameter.sort() accepts two optional parameters.It sorts list elements in either ascending or descending order.

