Open In App
Related Articles

Python – Difference between sorted() and sort()

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Sorting means rearranging a given sequence of elements according to a comparison operator on the elements. The comparison operator is used to decide the new order of the elements in the respective data structure.

For example, The below list of characters is sorted in increasing order of their ASCII values. That is, the character with a lesser ASCII value will be placed first than the character with a higher ASCII value.

python-sorting

In Python, sorting any sequence is very easy as it provides in-built methods for sorting. Two such methods are sorted() and sort(). These two methods are used for sorting but are quite different in their way. Let’s have a look at them one by one.

What is sorted() Method in Python?

sorted() method sorts the given sequence as well as set and dictionary(which is not a sequence) either in ascending order or in descending order(does Unicode comparison for string char by char) and always returns a sorted list. This method doesn’t affect the original sequence.

Syntax: sorted(iterable, key, reverse=False) 

What is sort() Method in Python?

sort() in Python function is very similar to sorted() but unlike sorted it returns nothing and makes changes to the original sequence. Moreover, sort() in Python is a method of list class and can only be used with lists. 

Syntax: List_name.sort(key, reverse=False)

Difference between sorted() and sort() in Python

Here, we are discussing Difference between sorted() and sort() on below points with the help of the code examples.

Sorting Number in a List

sorted() Method: In this example, below code uses the sorted list by arranging its elements in order. After that, it prints the original list to prove that the order of the elements in the original list hasn’t changed.

Python3

L = [1, 5, 4, 2, 3]
 
#Print the sorted list
print("Sorted list:")
print(sorted(L))
 
#Print the original list
print("\nOriginal list after sorting:")
print(L)

                    

Output:

Sorted list:
[1, 2, 3, 4, 5]

Original list after sorting:
[1, 5, 4, 2, 3]

sort() Method: In this example , below code takes a list of numbers and sorts it directly, changing the order of the elements in the original list. It then prints the sorted list .

Python3

L = [1, 5, 4, 2, 3]
 
# Sorting the list in-place using sort()
L.sort()
 
# Print the sorted list
print("Sorted list:")
print(L)
 
# Print the original list after sorting
print("\nOriginal list after sorting:")
print(L)

                    

Output:

Sorted list:
[1, 2, 3, 4, 5]

Original list after sorting:
[1, 2, 3, 4, 5]

Sorting String in a List

sorted() Method: In this example , below code shows use of the `sorted()` function with different data types. First, it sorts a list of characters alphabetically, then it sorts a tuple, sorts the characters in a string based on their ASCII values, providing an ordered representation for each data type.

Python3

# List
x_list = ['q', 'w', 'r', 'e', 't', 'y']
print("Original List:", x_list)
sorted_list = sorted(x_list)
print("Sorted List:", sorted_list)
 
# Tuple
x_tuple = ('q', 'w', 'e', 'r', 't', 'y')
print("\nOriginal Tuple:", x_tuple)
sorted_tuple = sorted(x_tuple)
print("Sorted Tuple:", sorted_tuple)

                    

Output:

Original List: ['q', 'w', 'r', 'e', 't', 'y']
Sorted List: ['e', 'q', 'r', 't', 'w', 'y']

Original Tuple: ('q', 'w', 'e', 'r', 't', 'y')
Sorted Tuple: ['e', 'q', 'r', 't', 'w', 'y']

sort() Method: In this example, below code sorts a list of characters in-place using the `sort()` method, and then, for a tuple and a string, it converts them to lists, sorts, and prints the elements in ascending order, demonstrating the modification of the original order in each case.

Python3

# List
x_list = ['q', 'w', 'r', 'e', 't', 'y']
print("Original List:", x_list)
x_list.sort()
print("Sorted List:", x_list)
 
# Tuple (converted to a list for sorting)
x_tuple = ('q', 'w', 'e', 'r', 't', 'y')
print("\nOriginal Tuple:", x_tuple)
x_list_from_tuple = list(x_tuple)
x_list_from_tuple.sort()
print("Sorted Tuple:", x_list_from_tuple)

                    

Output :

Original List: ['q', 'w', 'r', 'e', 't', 'y']
Sorted List: ['e', 'q', 'r', 't', 'w', 'y']

Original Tuple: ('q', 'w', 'e', 'r', 't', 'y')
Sorted Tuple: ['e', 'q', 'r', 't', 'w', 'y']

Reverse Order Sorting with Different Datatype

Using Sort() Function

In this example Python code demonstrates the use of the `sort()` method to sort lists of integers, floating-point numbers, and strings in descending order. Here, the lists (`numbers`, `decimalnumber`, and `words`) are sorted in reverse, and the sorted results are printed to showcase descending sorting for each data type.

Python3

# Using sort() with reverse for List of Integers, Floating Point Numbers, and Strings
 
# List of Integers
numbers = [1, 3, 4, 2]
print("Original List:", numbers)
numbers.sort(reverse=True)
print("Sorted List (Descending Order):", numbers)
 
# List of Floating Point Numbers
decimal_numbers = [2.01, 2.00, 3.67, 3.28, 1.68]
print("\nOriginal List:", decimal_numbers)
decimal_numbers.sort(reverse=True)
print("Sorted List (Descending Order):", decimal_numbers)
 
# List of Strings
words = ["Geeks", "For", "Geeks"]
print("\nOriginal List:", words)
words.sort(reverse=True)
print("Sorted List (Descending Order):", words)

                    

Output:

Original List: [1, 3, 4, 2]
Sorted List (Descending Order): [4, 3, 2, 1]

Original List: [2.01, 2.0, 3.67, 3.28, 1.68]
Sorted List (Descending Order): [3.67, 3.28, 2.01, 2.0, 1.68]

Original List: ['Geeks', 'For', 'Geeks']
Sorted List (Descending Order): ['Geeks', 'Geeks', 'For']

Using Sorted() Function

In this example Python code demonstrates the use of the `sorted()` method to sort lists of integers, floating-point numbers, and strings in descending order. Here, the lists (`numbers`, `decimalnumber`, and `words`) are sorted in reverse, and the sorted results are printed to showcase descending sorting for each data type.

Python3

# Using sorted() with reverse for List of Integers, Floating Point Numbers, and Strings
 
# List of Integers
numbers = [1, 3, 4, 2]
print("Original List:", numbers)
sorted_numbers = sorted(numbers, reverse=True)
print("Sorted List (Descending Order):", sorted_numbers)
 
# List of Floating Point Numbers
decimal_numbers = [2.01, 2.00, 3.67, 3.28, 1.68]
print("\nOriginal List:", decimal_numbers)
sorted_decimal_numbers = sorted(decimal_numbers, reverse=True)
print("Sorted List (Descending Order):", sorted_decimal_numbers)
 
# List of Strings
words = ["Geeks", "For", "Geeks"]
print("\nOriginal List:", words)
sorted_words = sorted(words, reverse=True)
print("Sorted List (Descending Order):", sorted_words)

                    

Output :

Original List: [1, 3, 4, 2]
Sorted List (Descending Order): [4, 3, 2, 1]

Original List: [2.01, 2.0, 3.67, 3.28, 1.68]
Sorted List (Descending Order): [3.67, 3.28, 2.01, 2.0, 1.68]

Original List: ['Geeks', 'For', 'Geeks']
Sorted List (Descending Order): ['Geeks', 'Geeks', 'For']

Related Article:

  1. sort() in Python
  2. sorted() in Python

Let us see the differences in a tabular form:

Sort vs Sorted Performance

sorted()sort()
The sorted() function returns a sorted list of the specific iterable object.The sort() method sorts the list.
We can specify ascending or descending order while using the sorted() functionIt sorts the list in ascending order by default.

Its Syntax is :

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

Its Syntax is :

list.sort(reverse=True|False, key=myFunc)

Its return type is a sorted list.We can also use it for sorting a list in descending order.

Does not modify the original iterable

Modifies the original list in-place

Accepts additional parameters such as key and reverse for customization

Accepts key and reverse parameters directly

It can only sort a list that contains only one type of value.It sorts the list in-place.

Can handle different types of iterables

Limited to lists, raises an error for other types

Compatible with any iterable

Only applicable to lists

Can be used with strings for alphabetical sorting

Directly applicable to lists of strings



Last Updated : 17 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads