# Python list sort()

The sort function can be used to sort a list in ascending, descending or user defined order.
To sort the list in ascending order.

List_name.sort()
This will sort the given list in ascending order.

This function can be used to sort list of integers, floating point number, string and others.

 `numbers ``=` `[``1``, ``3``, ``4``, ``2``] ` ` `  `# Sorting list of Integers in ascending ` `numbers.sort() ` ` `  `print``(numbers) `

Output:

```[1, 2, 3, 4]
```

 `strs ``=` `[``"geeks"``, ``"code"``, ``"ide"``, ``"practice"``] ` ` `  `# Sorting list of Integers in ascending ` `strs.sort() ` ` `  `print``(strs) `

Output:

```['code', 'geeks', 'ide', 'practice']
```

To sort the list in descending order.

list_name.sort(reverse=True)
This will sort the given list in descending order.

 `numbers ``=` `[``1``, ``3``, ``4``, ``2``] ` ` `  `# Sorting list of Integers in descending ` `numbers.sort(reverse ``=` `True``) ` ` `  `print``(numbers) `

Output:

```[4, 3, 2, 1]
```

Sorting user using user defined order.

list_name.sort(key=…, reverse=…) – it sorts according to user’s choice

 `# Python program to demonstrate sorting by user's ` `# choice ` ` `  `# function to return the second element of the ` `# two elements passed as the parameter ` `def` `sortSecond(val): ` `    ``return` `val[``1``]  ` ` `  `# list1 to demonstrate the use of sorting  ` `# using using second key  ` `list1 ``=` `[(``1``, ``2``), (``3``, ``3``), (``1``, ``1``)] ` ` `  `# sorts the array in ascending according to  ` `# second element ` `list1.sort(key ``=` `sortSecond)  ` `print``(list1) ` ` `  `# sorts the array in descending according to ` `# second element ` `list1.sort(key ``=` `sortSecond, reverse ``=` `True``) ` `print``(list1) `

Output:

```[(1, 1), (1, 2), (3, 3)]
[(3, 3), (1, 2), (1, 1)]
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : shubham_singh

Article Tags :
Practice Tags :

9

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.