๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ howto โ€บ sorting.html
Sorting Techniques โ€” Python 3.14.4 documentation
February 23, 2026 - >>> sorted("This is a test string from Andrew".split(), key=str.casefold) ['a', 'Andrew', 'from', 'is', 'string', 'test', 'This'] The value of the key parameter should be a function (or other callable) that takes a single argument and returns a key to use for sorting purposes. This technique is fast because the key function is called exactly once for each input record. A common pattern is to sort complex objects using some of the objectโ€™s indices as keys. For example:
๐ŸŒ
Google
developers.google.com โ€บ google for education โ€บ python โ€บ python sorting
Python Sorting | Python Education | Google for Developers
## Say we have a list of strings we want to sort by the last letter of the string. strs = ['xc', 'zb', 'yd' ,'wa'] ## Write a little function that takes a string, and returns its last letter.
Discussions

How to sort the letters in a string alphabetically in Python - Stack Overflow
Is there an easy way to sort the letters in a string alphabetically in Python? So for: a = 'ZENOVW' I would like to return: 'ENOVWZ' More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to sort a string alphabetically?
''.join(sorted(str)) More on reddit.com
๐ŸŒ r/learnpython
3
7
February 13, 2021
How to sort a list of strings according to a substring contained within each strings?
Sort by name first, then sort by length. data.sort() # sort by name data.sort(key=len) #sort by length You could combine those 2 steps into 1 with a lambda function that returns a tuple of (length, name) if you want to be fancy. More on reddit.com
๐ŸŒ r/learnpython
9
19
June 17, 2022
python - How to sort a list of strings? - Stack Overflow
Use locale and it's string collation methods to sort naturally according to current locale. ... This modifies your original list (i.e. sorts in-place). To get a sorted copy of the list, without changing the original, use the sorted() function: ... However, the examples above are a bit naive, ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-program-to-sort-a-string
Python Program to Sort a String - GeeksforGeeks
July 11, 2025 - sorted() function is the most efficient way to sort a string. It returns a sorted list of characters which we can then join back into a single string. This method is concise and efficient as it uses Python's built-in functions.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_sorted.asp
Python sorted() Function
Remove List Duplicates Reverse a String Add Two Numbers ยท Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... The sorted() function returns a sorted list of the specified iterable object.
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ how to sort a string in python?
How to Sort a String in Python | Analytics Vidhya
October 17, 2024 - How to Sort a String in Python? Characters in specific orders, utilizing methods like sorted() function, bubble sort, and list sort().
๐ŸŒ
Replit
replit.com โ€บ home โ€บ discover โ€บ how to sort a string in python
How to sort a string in Python | Replit
February 13, 2026 - Here, youโ€™ll find several techniques to sort strings, with practical tips, real-world applications, and debugging advice to help you master string manipulation for your specific use cases. text = "python" sorted_text = ''.join(sorted(text)) print(sorted_text)--OUTPUT--hnopty
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-how-to-sort-a-list-of-strings
How to sort a list of strings in Python - GeeksforGeeks
July 11, 2025 - Explanation: key=len sorts the strings based on their length in ascending order. We can also use any custom sorting logic using a key function.
Find elsewhere
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ how to sort list of strings in python
How to Sort List of Strings in Python - Spark By {Examples}
May 31, 2024 - For example, the sort() method is called on the my_list object, and it sorts the elements of the list in ascending order. If you want to sort the list in descending order, you can use the reverse=True parameter ยท In this article, I have explained ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to sort a string alphabetically?
r/learnpython on Reddit: How to sort a string alphabetically?
February 13, 2021 -

If I have a string "cab", I want to sort the string alphabetically to get "abc". The sorted() function returns a list instead of a string. For example, I tried the following:

str = "cab"
sorted_str = sorted(str)
print(sorted_str)

and it prints ['a', 'b', 'c'] instead of 'abc'. I can use a for loop to create another string like this and it prints 'abc', but it's cumbersome:

new_sorted_str = ''
for i in range(len(sorted_str)):
    new_sorted_str += sorted_str[i]
print(new_sorted_str) 

Is there a better way?

.

๐ŸŒ
Real Python
realpython.com โ€บ python-sort
How to Use sorted() and .sort() in Python โ€“ Real Python
February 24, 2025 - If youโ€™re curious about how sets work in Python, then you can check out the tutorial Sets in Python. Just like lists, tuples, and sets, strings are also iterables. This means you can sort str types as well. The example below shows how sorted() iterates through each character in the value ...
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python string sort
Python String Sort - Spark By {Examples}
May 21, 2024 - You can sort a string in Python using many ways, for example, by using join(), sorted(), reduce(), lambda, and for loop.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to sort a list of strings according to a substring contained within each strings?
r/learnpython on Reddit: How to sort a list of strings according to a substring contained within each strings?
June 17, 2022 -

I have a list of strings, which are the file names in a folder, that looks something like this:

['data_page10of10.png', 
'data_page1of10.png', 
'data_page2of10.png', 
'data_page3of10.png', 
'data_page4of10.png', 
'data_page5of10.png', 
'data_page6of10.png', 
'data_page7of10.png', 
'data_page8of10.png', 
'data_page9of10.png' ]

How can I sort this list so that it goes page1, page2, page3... (instead of page10 being at the front)? Also, I don't know that there will necessarily be 10 pages in the folder.

I'm guessing that I want to look at the substring that occurs between "page" and "of" for each one, and then sort them by that. How would I do this?

Top answer
1 of 11
597

Basic answer:

mylist = ["b", "C", "A"]
mylist.sort()

This modifies your original list (i.e. sorts in-place). To get a sorted copy of the list, without changing the original, use the sorted() function:

for x in sorted(mylist):
    print x

However, the examples above are a bit naive, because they don't take locale into account, and perform a case-sensitive sorting. You can take advantage of the optional parameter key to specify custom sorting order (the alternative, using cmp, is a deprecated solution, as it has to be evaluated multiple times - key is only computed once per element).

So, to sort according to the current locale, taking language-specific rules into account (cmp_to_key is a helper function from functools):

sorted(mylist, key=cmp_to_key(locale.strcoll))

And finally, if you need, you can specify a custom locale for sorting:

import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') # vary depending on your lang/locale
assert sorted((u'Ab', u'ad', u'aa'),
  key=cmp_to_key(locale.strcoll)) == [u'aa', u'Ab', u'ad']

Last note: you will see examples of case-insensitive sorting which use the lower() method - those are incorrect, because they work only for the ASCII subset of characters. Those two are wrong for any non-English data:

# this is incorrect!
mylist.sort(key=lambda x: x.lower())
# alternative notation, a bit faster, but still wrong
mylist.sort(key=str.lower)
2 of 11
64

It is also worth noting the sorted() function:

for x in sorted(list):
    print x

This returns a new, sorted version of a list without changing the original list.

๐ŸŒ
datagy
datagy.io โ€บ home โ€บ python posts โ€บ python strings โ€บ python: sort a string (4 different ways)
Python: Sort a String (4 Different Ways) โ€ข datagy
December 19, 2022 - Learn how to use Python to sort a string including how to sort strings using the sorted function, with case sensitivity or insensitivity.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python string sort alphabetically
Python String Sort Alphabetically - Spark By {Examples}
May 21, 2024 - In this example, this function works with the reduce() function from the functools module and gives the desired output. First, the sorted() function sorts the characters of the input string string alphabetically and then returns them as a list.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ sort a string in python
Sort a String in Python - Scaler Topics
April 7, 2024 - This article by Scaler topics will discuss about how to sort a string in Python. Sorting a string using a while loop and for a loop.
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-sort.html
Python Sorting
>>> sorted(strs, reverse=True) ['zebra', 'donut', 'banana', 'apple'] By default in Python, uppercase chars come before lowercase chars, so uppercase strings will sort to the front of the list:
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ examples โ€บ alphabetical-order
Python Program to Sort Words in Alphabetic Order
To understand this example, you should have the knowledge of the following Python programming topics: ... In this example, we illustrate how words can be sorted lexicographically (alphabetic order). # Program to sort alphabetically the words form a string provided by the user my_str = "Hello this Is an Example With cased letters" # To take input from the user #my_str = input("Enter a string: ") # breakdown the string into a list of words words = [word.lower() for word in my_str.split()] # sort the list words.sort() # display the sorted words print("The sorted words are:") for word in words: print(word)
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-sort-numeric-strings-in-a-list
Sort Numeric Strings in a List - Python - GeeksforGeeks
July 11, 2025 - For example, if we have: a = ["10", "2", "30", "4"] then the expected output should be: ["2", "4", "10", "30"] because numerically, 2 < 4 < 10 < 30. We use Python's built-in sorted() function along with the key=int parameter as this converts ...