How to sort the objects in a list in Python?

In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In other programming languages, a list is equivalent to an array. Square brackets are used to denote it, and a comma (,) is used to divide two items in the list.

In this article, we will show you how to sort the objects and elements in a list using Python. Below are the different methods to accomplish this task ?

  • Using sort() method

  • Using sorted() method

Assume we have taken a list containing some elements. We will return the list elements after sorting either in ascending or descending order.

NOTE ? If the object/element of the list is a string the elements are sorted in alphabetical order.

Using sort() Method

The sort() method sorts the original list in place. It signifies that the sort() method changes the order of the list's elements permanently.

By default, the sort() method uses the less-than operator (<) to sort the entries of a list i.e, in ascending order. In other words, it prioritizes lesser elements above higher ones.

To sort elements from highest to lowest (descending order), use the reverse=True parameter in the sort() method ?

list.sort(reverse=True)

Example 1

The following program sorts the list elements in ascending and descending order using the sort() method ?

# input list
numbers = [10, 4, 12, 1, 9, 5]

# sorting elements of the list in ascending order
numbers.sort()
print("Sorting list items in ascending order: ", numbers)

# sorting elements of the list in descending order
numbers.sort(reverse=True)
print("Sorting list items in descending order: ", numbers)
Sorting list items in ascending order:  [1, 4, 5, 9, 10, 12]
Sorting list items in descending order:  [12, 10, 9, 5, 4, 1]

In this case, we've given a list of random values. The sort() method was then applied to the list, which sorted the given list in ascending order and printed the list in ascending order. The same list was then sorted in descending order by passing an additional parameter to the sort() function, reverse=True, and the list was printed in descending order.

Example 2: For a List Containing String Values

The following program sorts the list elements (string) in ascending and descending order using the sort() method ?

# input list
words = ['hello', 'this', 'is', 'tutorials', 'point', 'website', 'welcome', 'all']

# sorting string elements of the list in ascending order
words.sort()
print("Sorting list items in ascending order: ", words)

# sorting string elements of the list in descending order
words.sort(reverse=True)
print("Sorting list items in descending order: ", words)
Sorting list items in ascending order:  ['all', 'hello', 'is', 'point', 'this', 'tutorials', 'website', 'welcome']
Sorting list items in descending order:  ['welcome', 'website', 'tutorials', 'this', 'point', 'is', 'hello', 'all']

We can see that all the elements are sorted in alphabetical order.

Using sorted() Method

The sorted() function returns a sorted list of the iterable object given. Unlike sort(), it creates a new sorted list without modifying the original list.

You can choose between ascending and descending order. Numbers are sorted numerically, while strings are arranged alphabetically.

Syntax

sorted(iterable, key=key, reverse=reverse)

Parameters

iterable ? It is a sequence

key ? A function that will be executed to determine the order. The default value is None.

reverse ? A Boolean expression. False sorts ascending (default), True sorts descending.

Example

The following program sorts the list elements in ascending and descending order using the sorted() method ?

# input list
numbers = [10, 4, 12, 1, 9, 5]

# sorting elements of the list in ascending order
print("Original list:", numbers)
print("Sorting list items in ascending order: ", sorted(numbers))

# sorting elements of the list in descending order
print("Sorting list items in descending order: ", sorted(numbers, reverse=True))

# Original list remains unchanged
print("Original list after sorting:", numbers)
Original list: [10, 4, 12, 1, 9, 5]
Sorting list items in ascending order:  [1, 4, 5, 9, 10, 12]
Sorting list items in descending order:  [12, 10, 9, 5, 4, 1]
Original list after sorting: [10, 4, 12, 1, 9, 5]

In this example, we've provided a set of random numbers. We gave the list as an argument to the sorted() method, which sorted and printed the given list in ascending order. The same list was then sorted in descending order by giving an additional parameter, reverse=True, to the sorted() method. Notice how the original list remains unchanged.

Comparison

Method Modifies Original List? Returns Value? Best For
sort() Yes None When you want to sort in-place
sorted() No New sorted list When you want to preserve original list

Conclusion

Use sort() to modify the original list permanently and sorted() to create a new sorted list while preserving the original. Both methods support the reverse=True parameter for descending order sorting.

Updated on: 2026-03-24T19:03:58+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements