Sorting is a fundamental operation in programming, and Python makes it easy to sort lists containing numbers and strings. In this post, we will explore different ways to sort lists in both ascending and descending order using Python.

1. Sorting Numbers in Ascending and Descending Order

When working with numerical data, sorting a list in ascending (smallest to largest) or descending (largest to smallest) order is common.

Q: How do I sort a list of numbers in ascending order?

To sort numbers in ascending order, we use the built-in sorted() function or the .sort() method.

numbers = [42, 7, 23, 100, 8]
sorted_numbers = sorted(numbers)  # Ascending order
print(sorted_numbers)

Output:

[7, 8, 23, 42, 100]

Q: How do I sort a list of numbers in descending order?

To sort numbers in descending order, simply add reverse=True.

numbers = [42, 7, 23, 100, 8]
sorted_numbers = sorted(numbers, reverse=True)  # Descending order
print(sorted_numbers)

Output:

[100, 42, 23, 8, 7]

Alternatively, you can use the .sort() method to modify the list in place:

numbers.sort(reverse=True)
print(numbers)

2. Sorting Strings in Ascending and Descending Order

Python also allows sorting lists that contain strings. Strings are sorted lexicographically, meaning in dictionary order.

Q: How do I sort a list of strings in alphabetical order (A to Z)?

Use sorted() to sort in ascending order (A → Z):

words = ["banana", "apple", "grape", "orange", "kiwi"]
sorted_words = sorted(words)  # Ascending order
print(sorted_words)

Output:

['apple', 'banana', 'grape', 'kiwi', 'orange']

Q: How do I sort a list of strings in reverse alphabetical order (Z to A)?

Add reverse=True to sort in descending order (Z → A):

sorted_words = sorted(words, reverse=True)
print(sorted_words)

Output:

['orange', 'kiwi', 'grape', 'banana', 'apple']

3. Sorting Strings That Contain Numbers

Sometimes, lists contain strings with numbers, like ["M10", "M2", "M1", "M20"]. By default, Python sorts them as strings, not numerically.

Q: Why doesn’t Python sort string numbers correctly?

If we sort ["M10", "M2", "M1", "M20"] normally, we get:

codes = ["M10", "M2", "M1", "M20", "M3"]
sorted_codes = sorted(codes)
print(sorted_codes)

Output (incorrect for numerical sorting):

['M1', 'M10', 'M2', 'M20', 'M3']

This happens because Python sorts strings based on characters, so "M10" comes before "M2" because "1" is smaller than "2" in string comparison.

Q: How do I sort a list of strings based on numeric values inside them?

To sort correctly by numbers, extract the digits using re.search() and sort accordingly:

import re

codes = ["M10", "M2", "M1", "M20", "M3"]
sorted_codes = sorted(codes, key=lambda x: int(re.search(r'\d+', x).group()))
print(sorted_codes)

Output (correct numerical order):

['M1', 'M2', 'M3', 'M10', 'M20']

Q: How do I sort a list of strings with numbers in descending order?

Simply add reverse=True:

sorted_codes_desc = sorted(codes, key=lambda x: int(re.search(r'\d+', x).group()), reverse=True)
print(sorted_codes_desc)

Output:

['M20', 'M10', 'M3', 'M2', 'M1']

Conclusion

Sorting lists in Python is simple but varies depending on whether you are dealing with numbers, strings, or mixed content. Here’s a quick summary:

✅ Use sorted() for a new sorted list and .sort() to modify in place.
✅ For numerical sorting, sorted(numbers, reverse=True) works best.
✅ For string sorting, Python follows dictionary order.
✅ To sort strings with numbers correctly, extract the numeric values using regex.

Now it’s your turn! Have you encountered challenges while sorting lists in Python? Let us know in the comments! 🚀

Share this if you like: