Sorting a list in Python: the difference between sorted and sorted

Money and Business

There are two ways to sort a list in ascending or descending order in Python.

  • sort()
  • sorted()

If you want to sort a string or tuple, use sorted().

The following information is provided here.

  • A method of type list that sorts the original listsort()
  • Generate a new sorted list, built-in function: .sorted()
  • How to sort strings and tuples

Sorting the original list, a method of type list: sort()

sort() is a list type method.

A destructive process in which the original list itself is rewritten.

org_list = [3, 1, 4, 5, 2]

org_list.sort()
print(org_list)
# [1, 2, 3, 4, 5]

Note that sort() returns None.

print(org_list.sort())
# None

The default is ascending order. If you want to sort in descending order, set the argument reverse to true.

org_list.sort(reverse=True)
print(org_list)
# [5, 4, 3, 2, 1]

Generate a new sorted list, built-in function: sorted()

sorted() is a built-in function.

Returns a sorted list when the list to be sorted is specified as an argument. This is a nondestructive process that does not change the original list.

org_list = [3, 1, 4, 5, 2]

new_list = sorted(org_list)
print(org_list)
print(new_list)
# [3, 1, 4, 5, 2]
# [1, 2, 3, 4, 5]

As with sort(), the default is ascending order. If you want to sort in descending order, set the argument reverse to true.

new_list_reverse = sorted(org_list, reverse=True)
print(org_list)
print(new_list_reverse)
# [3, 1, 4, 5, 2]
# [5, 4, 3, 2, 1]

How to sort strings and tuples

Since strings and tuples are immutable, there is no sort() method available to rewrite the original object.

On the other hand, the argument of the sorted() function, which generates a sorted list as a new object, can be a string or tuple as well as a list. However, since sorted() returns a list, it needs to be converted to a string or tuple.

Sorting strings

When a string is specified as the argument of the sorted() function, a list is returned in which each character of the sorted string is stored as an element.

org_str = 'cebad'

new_str_list = sorted(org_str)
print(org_str)
print(new_str_list)
# cebad
# ['a', 'b', 'c', 'd', 'e']

To concatenate a list of strings into a single string, use the join() method.

new_str = ''.join(new_str_list)
print(new_str)
# abcde

If you want to sort in descending order, set the argument reverse to true.

new_str = ''.join(sorted(org_str))
print(new_str)
# abcde

new_str_reverse = ''.join(sorted(org_str, reverse=True))
print(new_str_reverse)
# edcba

The size of a string is determined by the Unicode code point (character code) of the character.

Sorting tuples

Tuples are the same as strings; specifying a tuple as the argument of the sorted() function returns a sorted list of elements.

org_tuple = (3, 1, 4, 5, 2)

new_tuple_list = sorted(org_tuple)
print(org_tuple)
print(new_tuple_list)
# (3, 1, 4, 5, 2)
# [1, 2, 3, 4, 5]

To convert a list to a tuple, use tuple().

new_tuple = tuple(new_tuple_list)
print(new_tuple)
# (1, 2, 3, 4, 5)

If you want to sort in descending order, set the argument reverse to true.

new_tuple = tuple(sorted(new_tuple_list))
print(new_tuple)
# (1, 2, 3, 4, 5)

new_tuple_reverse = tuple(sorted(new_tuple_list, reverse=True))
print(new_tuple_reverse)
# (5, 4, 3, 2, 1)