Last Updated on July 13, 2023 by Mayank Dham

Python, a widely adopted programming language, finds application in diverse domains such as web development, machine learning, data analysis, and more. It offers an extensive collection of built-in functions that enhance coding productivity and effectiveness. Among these functions, the sort() function holds significance as it facilitates the sorting of elements within a list, thereby enabling better organization and arrangement of data.

This article delves into the Python sort() function, encompassing its syntax, the return value it produces, and practical examples illustrating its usage. Additionally, we have included a set of frequently asked questions (FAQs) to aid in your comprehension of this function’s functionality.

## What is Sort() Function in Python?

The sort() function in Python serves the purpose of arranging the elements within a list either in ascending or descending order. It is applicable for sorting various data types such as numbers, strings, and more. It’s important to note that the sort() function modifies the original list and does not generate a new one.

The sort function in Python employs a "stable sort" algorithm to arrange the elements of a list. A stable sort algorithm ensures that the relative order of equal elements remains unchanged. For instance, if there are two elements, "a" and "b," in a list where "a" appears before "b," the sort() function will preserve this order even after the list is sorted.

### Syntax of Sort Function in Python

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

`list.sort(key=None, reverse=False)`

Here, list is the list that needs to be sorted. The key and reverse parameters are optional.

### Parameter of Sort Function in Python

The **key** parameter is a function that is used to customize the sorting order. If key is specified, it will be used to extract a comparison key from each element in the list. The sorting will be done based on the comparison key instead of the original element.

The **reverse** parameter is a boolean value that determines whether the list should be sorted in ascending or descending order. If reverse is True, the list will be sorted in descending order. Otherwise, it will be sorted in ascending order.

### Python Sort Function Time Complexity

Python sort() function time complexity is O(n log n) for average and worst cases. However, for the best case scenario where the list is already sorted, the time complexity reduces to O(n).

### Return Value of Sort Function in Python

The sort() function in Python does not return anything. It sorts the list in place, which means that it changes the original list and does not create a new one.

## Examples of Sort Function in Python

Let’s take a look at some examples of how to use the sort function in Python:

**Example 1: Sort Function in Python**

Here we Sort a list of numbers in ascending order

**Code Implementation:**

```
numbers = [4, 11, 6, 12, 3, 5]
numbers.sort()
print(numbers)
```

**Output:**

`[3,4,5,6,11,12]`

**Explanation:**

In this example, we have a list of numbers [4, 11, 6, 12, 3, 5]. We use the sort() function to sort the list in ascending order. The print() function is used to display the sorted list.

**Example 2: Sort Function in Python**

Here we Sort a list of numbers in descending order

```
numbers = [4, 2, 11, 1, 3, 9]
numbers.sort(reverse=True)
print(numbers)
```

**Output:**

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

**Explanation:**

In this example, we have a list of numbers [4, 2, 11, 1, 3, 9]. We use the sort() function with the reverse=True parameter to sort the list in descending order. The print() function is used to display the sorted list.

**Example 3: Sort Function in Python**

Here we Sorting a list of strings

```
fruits = ["banana", "apple", "cherry", "kiwi", "mango"]
fruits.sort()
print(fruits)
```

**Output:**

`['apple', 'banana', 'cherry', 'kiwi', 'mango']`

**Explanation:**

In this example, we create a list of strings and use the sort() function to sort them in ascending order. The sort() function modifies the original fruits list in place and sorts it in alphabetical order.

**Example 4: Sort Function in Python**

Sorting a list based on the length of strings

```
fruits = ["banana", "apple", "cherry", "kiwi", "mango"]
fruits.sort(key=len)
print(fruits)
```

**Output:**

`['kiwi', 'mango', 'apple', 'banana', 'cherry']`

**Explanation**

In this example, we create a list of strings and use the sort() function with the key=len parameter to sort them based on the length of each string. The sort() function modifies the original fruits list in place and sorts it based on the length of each string.

**Summary**

The sort() function in Python is a valuable tool for sorting elements within a list. It allows for the arrangement of items in either ascending or descending order, making it applicable to various data types. The function modifies the original list and does not create a new one. Understanding how to use the sort() function effectively enhances data organization and manipulation in Python.

## Sort Function in Python – FAQs

Here are the some FAQs on sort function in python

**Q1: Can the sort() function sort a list of custom objects?**

**Ans:** Yes, the sort() function can sort a list of custom objects, but it requires a custom key function that defines the sorting order. The key function should return a value that can be used to compare the objects in the list.

**Q2: How does the sort() function compare strings?**

**Ans:** The sort() function compares strings based on their ASCII values. In other words, it sorts them alphabetically, with uppercase letters coming before lowercase letters.

**Q3: Does the sort() function modify the original list or create a new sorted list?**

**Ans:** The sort function in python modifies the original list in place and does not create a new sorted list.

**Q4: What is the difference between the sort() and sorted() functions in Python?**

**Ans:** The sort() function is a method of a list object that sorts the elements of a list in place, while the sorted() function is a built-in Python function that returns a new sorted list without modifying the original list.

**Q5: Can the sort() function sort a list in reverse order?**

**Ans**: Yes, the sort() function can sort a list in reverse order by setting the reverse parameter to True.

**Q6: Can the sort() function be used with other collection types, such as tuples or dictionaries?**

**Ans:** No, the sort() function is a method of a list object and can only be used with lists.