Python List sort() Method with Examples

How to Sort a List in Python

In this tutorial, you'll learn how to sort a list using the list sort() method in Python. Also, this tutorial illustrates sample programs on how to use the range() function and the FOR loop statement to sort a list of numbers, strings, and lists without using the list sort() method.

List sort() Method Syntax

To sort a list in Python, you use the following syntax:

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

Remarks

  • list: The list that you want to sort
  • The reverse argument defaults to False if omitted. If True, the list sorted in descending order.
  • The key argument defaults to None if omitted.

Examples

Sort a List of Numbers

The following code illustrates an example of how to sort a list of numbers:

# sort a list of numbers in ascending order
numbers = [200100400500300400700800600]
numbers.sort()
print(numbers)

Output:

[100, 200, 300, 400, 400, 500, 600, 700, 800]

Sort a List of Strings Alphabetically

The following program illustrates how to sort a list of strings alphabetically (letters and number):

# sort a list of strings alphabetically (letters and number)
brands = ["Microsoft""Oracle""Facebook""Twitter""1G""2M",
          "Apple""Google""Amazon""Ebay""Alibaba""Yahoo"]
brands.sort()
print(brands)

Output:

['1G', '2M', 'Alibaba', 'Amazon', 'Apple', 'Ebay', 'Facebook', 'Google', 'Microsoft', 'Oracle', 'Twitter', 'Yahoo']

Sort a List of Strings Alphabetically, in Descending Order

The following is an example of how to sort a list of strings alphabetically, in descending order:

# sort a list of strings alphabetically in descending order
brands = ["Microsoft""Oracle""Facebook""Twitter""1G""2M",
          "Apple""Google""Amazon""Ebay""Alibaba""Yahoo"]
brands.sort(reverse=True)
print(brands)

Output:

['Yahoo', 'Twitter', 'Oracle', 'Microsoft', 'Google', 'Facebook', 'Ebay', 'Apple', 'Amazon', 'Alibaba', '2M', '1G']

Sort a List of Strings by Length

The following is an example of how to sort a list of strings by length, in ascending order:

# sort a list of strings by length in ascending order
brands = ["Microsoft""Oracle""Facebook""Twitter""1G""2M",
          "Apple""Google""Amazon""Ebay""Alibaba""Yahoo"]
brands.sort(key=len)
print(brands)

Output:

['1G', '2M', 'Ebay', 'Apple', 'Yahoo', 'Oracle', 'Google', 'Amazon', 'Twitter', 'Alibaba', 'Facebook', 'Microsoft']

Sort a List of Strings by Length, in Descending Order

# sort a list of strings by length in descending order
brands = ["Microsoft""Oracle""Facebook""Twitter""1G""2M",
          "Apple""Google""Amazon""Ebay""Alibaba""Yahoo"]
brands.sort(key=len, reverse=True)
print(brands)

Output:

['Microsoft', 'Facebook', 'Twitter', 'Alibaba', 'Oracle', 'Google', 'Amazon', 'Apple', 'Yahoo', 'Ebay', '1G', '2M']

Sort a List of Lists

The following example illustrates how to sort a list of lists, by the first element of the sublists, in ascending order:

# sort a list of lists by the first element of the sublist, in ascending order
student_list1 = ["Addison""Jonh""Roland""Anthony""Olivia""Elizabeth"]
student_list2 = ["Jackson""Olivia""Jmaes""Emma""Christopher"]
student_list3 = ["Andrew""Olivia""James""Brooklyn"]
student_list = [student_list1, student_list2, student_list3]

student_list.sort()
print(student_list)

Output:

[['Addison', 'Jonh', 'Roland', 'Anthony', 'Olivia', 'Elizabeth'], ['Andrew', 'Olivia', 'James', 'Brooklyn'], ['Jackson', 'Olivia', 'Jmaes', 'Emma', 'Christopher']]

Sort a List of Lists by Length

The following code is an example of how to sort a list of lists by length, in ascending order:

# sort a list of lists by length, in ascending order
student_list1 = ["Addison""Jonh""Roland""Anthony""Olivia""Elizabeth"]
student_list2 = ["Jackson""Olivia""Jmaes""Emma""Christopher"]
student_list3 = ["Andrew""Olivia""James""Brooklyn"]
student_list = [student_list1, student_list2, student_list3]

student_list.sort(key=len)
print(student_list)

Output:

[['Andrew', 'Olivia', 'James', 'Brooklyn'], ['Jackson', 'Olivia', 'Jmaes', 'Emma', 'Christopher'], ['Addison', 'Jonh', 'Roland', 'Anthony', 'Olivia', 'Elizabeth']]

Sort a List of Lists by Length, in Descending Order

The following code is an example of how to sort a list of lists by length, in descending order:

# sort a list of lists by length, in descending order
student_list1 = ["Addison""Jonh""Roland""Anthony""Olivia""Elizabeth"]
student_list2 = ["Jackson""Olivia""Jmaes""Emma""Christopher"]
student_list3 = ["Andrew""Olivia""James""Brooklyn"]

student_list = [student_list1, student_list2, student_list3]
student_list.sort(key=len, reverse=True)
print(student_list)

Output:

[['Addison', 'Jonh', 'Roland', 'Anthony', 'Olivia', 'Elizabeth'], ['Jackson', 'Olivia', 'Jmaes', 'Emma', 'Christopher'], ['Andrew', 'Olivia', 'James', 'Brooklyn']]

Sort a List of Numbers without Using sort()

The following illustrates an example of how to write a program to sort a list of numbers in ascending order:

# how to sort numbers in python without the sort() function or method
# sort a list of numbers, in ascending order
numbers = []
numbers = [200100400500300400700800600]
length = len(numbers)
for i in range(length):
    for j in range(i + 1, length):
        if numbers[i] > numbers[j]:
            num1 = numbers[i]
            num2 = numbers[j]
            numbers[i] = num2
            numbers[j] = num1
print(numbers)

Output:

[100, 200, 300, 400, 400, 500, 600, 700, 800]

The following illustrates an example of how to write a program to sort a list of numbers in descending order:

# how to sort numbers in python without the sort() function or method
# sort a list of numbers, in descending order
numbers = []
numbers = [200100400500300400700800600]
length = len(numbers)
for i in range(length):
    for j in range(i + 1, length):
        if numbers[i] < numbers[j]:
            num1 = numbers[i]
            num2 = numbers[j]
            numbers[i] = num2
            numbers[j] = num1
print(numbers)

Output:

[800, 700, 600, 500, 400, 400, 300, 200, 100]

The following program sorts a list of strings in ascending order:

# how to sort a list alphabetically in python without the sort() function or method
# sort a list of fruits, in ascending order
fruits = ["Mango""Apple""Kiwi""Orange""Grape""Orange""Grapefruit"]
length = len(fruits)
for i in range(length):
    for j in range(i + 1, length):
        if fruits[i] > fruits[j]:
            f1 = fruits[i]
            f2 = fruits[j]
            fruits[i] = f2
            fruits[j] = f1
print(fruits)

Output:

['Apple', 'Grape', 'Grapefruit', 'Kiwi', 'Mango', 'Orange', 'Orange']

Here, the range() function and the FOR loop statement used to sort a list.

In this tutorial, you've learned how to use the list sort() method in Python. Also, this tutorial illustrated sample programs on how to sort a list without using the list sort() method.


See also:
Python return Statement with Examples
Python List Methods and Functions with Examples
Python abs() – Absolute Value in Python
Python Factorial – Find the Factorial of a Number
Python min() – Find the Smallest Value in Python

Leave a Comment