**Sorting refers to arranging the data in a particular order.** A tuple is a collection of data items separated by commas and enclosed within parentheses. The items in the tuple are ordered and immutable. For example, **tup_1 =** **(3, 9, 2, 7) ** represents a tuple. The tuple **tup_1** when sorted results in **(2, 3, 7, 9).**

This article discusses the different methods to sort the tuples.

We know that a tuple is immutable(unchangeable) and the sort() function modifies the original list. Therefore, to sort a tuple we convert the tuple to a list using the **list()** constructor. The list is sorted using the **sort()** function. The sorted list is converted back again to a tuple using the **tuple()** function.

The** sort()** function sorts the list in ascending order and can only be used with a list. The **sort()** function doesn't return anything. The two optional parameters for the **sort()** function are **key** and **reverse. **

The syntax for the **sort()** function is as follows.

```
#syntax:
list.sort(key = ..., reverse =...)
```

- The parameter
**reverse**takes a boolean value. If the**reverse**is set to**True**it sorts the list in descending order. - The parameter
**key**takes a function as a value for user-defined comparison.

Example,

```
tuple_1 = (7, 6, 3, 9)
print("tuple_1: ", tuple_1)
list_1 = list(tuple_1)
print("list_1: ", list_1)
list_1.sort()
print("Sorted Tuple: ", tuple(list_1))
```

The above code returns the output as

```
tuple_1: (7, 6, 3, 9)
list_1: [7, 6, 3, 9]
Sorted Tuple: (3, 6, 7, 9)
```

To sort the tuple in descending order we set the reverse parameter in the **sort()** function to True.

```
tuple_1 = (7, 6, 3, 9)
print("tuple_1: ", tuple_1)
list_1 = list(tuple_1)
print("list_1: ", list_1)
list_1.sort(reverse = True)
print("Sorted Tuple in descending order: ", tuple(list_1))
```

Output

```
tuple_1: (7, 6, 3, 9)
list_1: [7, 6, 3, 9]
Sorted Tuple in descending order: (9, 7, 6, 3)
```

**sorted()** function is a built-in function in python that takes three arguments** sequence(list, tuple, set,etc.) key(optional), reverse(optional) and returns a sorted list.** The major difference between the **sort()** and **sorted()** function is, **sorted()** function returns a sorted list and doesn't modify the original sequence.

The syntax for the **sorted()** function is as follows.

```
#syntax:
sorted(seq, key=..., reverse=...)
```

- The parameter
**reverse**takes a boolean value. If the**reverse**is set to**True**it sorts the sequence in descending order. - The parameter
**key**takes a function as a value for user-defined comparison.

A **sorted()** function returns a list. To get a sorted tuple, we typecast the list to a tuple using the **tuple()** function.

```
tuple_1 = (7, 6, 3, 9)
print("tuple_1: ", tuple_1)
sort_tup = tuple(sorted(tuple_1))
print("Sorted Tuple: ", sort_tup)
```

Output

```
tuple_1: (7, 6, 3, 9)
Sorted Tuple: (3, 6, 7, 9)
```

A tuple is immutable. Therefore, to sort a tuple we convert the tuple to a list using the** list()** function. Two for loops are used to repeatedly swap the elements in the list in each pass if they are not in ascending order. The sorted list is converted back to a tuple using the** tuple()** function.

```
tuple_1 = (3, 5, 8, 4.5)
print("tuple_1: ", tuple_1)
list_1 = list(tuple_1)
print("list_1 = ", list_1)
for i in range(len(list_1)):
for j in range(i+1, len(list_1)):
if list_1[i] > list_1[j]:
list_1[i] , list_1[j] = list_1[j] , list_1[i]
print("Sorted Tuple: ", tuple(list_1))
```

The above code returns the output as

```
tuple_1: (3, 5, 8, 4.5)
list_1 = [3, 5, 8, 4.5]
Sorted Tuple: (3, 4.5, 5, 8)
```

- What does .strip do in python
- How to convert a number to a list in python?
- How to count occurrences of an element in a list?
- How to check if a string is empty in python?
- How to capitalize the first letter in python?
- How to check the type of a variable in python?
- How to get absolute value in Python?
- How to get the last element of a list in python?