GFG App
Open App
Browser
Continue

# sort() in Python

The sort function can be used to sort the list in both ascending and descending order. It can be used to sort lists of integers, floating point numbers, strings, and others. Its time complexity is O(NlogN).

## Python sort() Syntax

The syntax of the sort() function in Python is as follows.

Syntax: list_name.sort(key=…, reverse=…)

Parameters:

By default, Python sort() doesn’t require any extra parameters and sorts the list in ascending order. However, it has two optional parameters:

• key:  function that serves as a key for the sort comparison
• reverse: If true, the list is sorted in descending order.

Return value: The sort() does not return anything but alters the original list according to the passed parameter.

## sort() in Python Examples

### Sorting List in Ascending Order

By default, the sort() in Python sorts a list in ascending order if we do not provide it with any parameters.

## Python3

 `# List of Integers` `numbers ``=` `[``1``, ``3``, ``4``, ``2``]`   `# Sorting list of Integers` `numbers.sort()`   `print``(numbers)`   `# List of Floating point numbers` `decimalnumber ``=` `[``2.01``, ``2.00``, ``3.67``, ``3.28``, ``1.68``]`   `# Sorting list of Floating point numbers` `decimalnumber.sort()`   `print``(decimalnumber)`   `# List of strings` `words ``=` `[``"Geeks"``, ``"For"``, ``"Geeks"``]`   `# Sorting list of strings` `words.sort()`   `print``(words)`

Output:

```[1, 2, 3, 4]
[1.68, 2.0, 2.01, 3.28, 3.67]
['For', 'Geeks', 'Geeks']```

### Sorting List in Descending Order

To sort a list in descending order, set the reverse parameter to True of the sort() function in Python.

## Python3

 `# List of Integers` `numbers ``=` `[``1``, ``3``, ``4``, ``2``]`   `# Sorting list of Integers` `numbers.sort(reverse``=``True``)`   `print``(numbers)`   `# List of Floating point numbers` `decimalnumber ``=` `[``2.01``, ``2.00``, ``3.67``, ``3.28``, ``1.68``]`   `# Sorting list of Floating point numbers` `decimalnumber.sort(reverse``=``True``)`   `print``(decimalnumber)`   `# List of strings` `words ``=` `[``"Geeks"``, ``"For"``, ``"Geeks"``]`   `# Sorting list of strings` `words.sort(reverse``=``True``)`   `print``(words)`

Output:

```[4, 3, 2, 1]
[3.67, 3.28, 2.01, 2.0, 1.68]
['Geeks', 'Geeks', 'For']```

### Sorting List with Custom Function

To sort the list according to the user’s choice, pass a function to the key parameter of the Python sort() function.

## Python3

 `# 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 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)]```

## Difference between sorted() and sort() function in Python

Let us see the difference between the sorted() and sort() function in Python:

To know more please refer Python difference between sorted() and sort() function.

My Personal Notes arrow_drop_up