# sort() in Python

## sort() in Python

Like C++ sort(), Java sort() and other languages, python also provides built in function to sort.

The sort function can be used to sort the list in both ascending and descending order.

To sort the list in ascending order.

Syntax

# This will sort the given list in ascending order.
# It returns a sorted list according to the passed parameter.
List_name.sort()

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

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

```

To sort the list in descending order.

Syntax

```list_name.sort(reverse=True)
This will sort the given list in descending order.
```
 `# 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']```

Syntax :

list_name.sort() – it sorts in ascending order
list_name.sort(reverse=True) – it sorts in descending order
list_name.sort(key=…, reverse=…) – it sorts according to user’s choice

Parameters:
By default, sort() doesn’t require any extra parameters. However, it has two optional parameters:

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

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

Last Updated on October 25, 2021 by admin

## Decorators with parameters in PythonDecorators with parameters in Python

Decorators with parameters in Python We know Decorators are a very powerful and useful tool in Python since it allows programmers to modify the behavior of function or class. In this article, we will learn about the Decorators with Parameters with help of multiple

## Create a DataFrame from a Numpy array and specify the index column and column headersCreate a DataFrame from a Numpy array and specify the index column and column headers

Create a DataFrame from a Numpy array and specify the index column and column headers Let us see how to create a DataFrame from a Numpy array. We will also learn how to specify the index and the column headers

## FuzzyWuzzy Python libraryFuzzyWuzzy Python library

FuzzyWuzzy Python library There are many methods of comparing string in python. Some of the main methods are: Using regex Simple compare Using difflib But one of the very easy method is by using fuzzywuzzy library where we can have a score

## Pandas – Parsing JSON DatasetPandas – Parsing JSON Dataset

Parsing JSON Dataset in Pandas Parsing of JSON Dataset using pandas is much more convenient. Pandas allow you to convert a list of lists into a Dataframe and specify the column names separately. A JSON parser transforms a JSON text

## Python Tkinter – Entry WidgetPython Tkinter – Entry Widget

Python Tkinter – Entry Widget Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. Python with Tkinter is the fastest and easiest way to create

## Check multiple conditions in if statement – PythonCheck multiple conditions in if statement – Python

Check multiple conditions in if statement – Python If-else conditional statement is used in Python when a situation leads to two conditions and one of them should hold true. Syntax: if (condition): code1 else: code2 [on_true] if [expression] else [on_false]

## Deque in PythonDeque in Python

Deque in Python Deque (Doubly Ended Queue) in Python is implemented using the module “collections“. Deque is preferred over list in the cases where we need quicker append and pop operations from both the ends of container, as deque provides

## Python – Display text to PyGame windowPython – Display text to PyGame window

Python | Display text to PyGame window Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s up to the