🌐
Python documentation
docs.python.org › 3 › howto › sorting.html
Sorting Techniques — Python 3.14.6 documentation
>>> 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:
🌐
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().
Discussions

python - How to sort a list of strings? - Stack Overflow
What is the best way of creating ... list in Python? ... Use locale and it's string collation methods to sort naturally according to current locale. ... Save this answer. ... Show activity on this post. ... 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 ... More on stackoverflow.com
🌐 stackoverflow.com
How does numeric string sorting work in python?
You are not sorting numbers though, you have a list of strings that may contain numbers, but they are still strings, so normal lexicographic order is used. More on reddit.com
🌐 r/learnpython
8
1
March 7, 2023
How to sort a string alphabetically?
''.join(sorted(str)) More on reddit.com
🌐 r/learnpython
3
7
February 13, 2021
How to sort IP Addresses in python
ips.sort(key=lambda s: map(int, s.split('.')))
More on reddit.com
🌐 r/Python
8
0
August 17, 2009
🌐
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.
🌐
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 ...
🌐
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.
🌐
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 Training ... The sorted() function returns a sorted list of the specified iterable object.
🌐
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.
🌐
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:
Find elsewhere
🌐
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.
🌐
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.
Top answer
1 of 11
598

Basic answer:

Copymylist = ["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:

Copyfor 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):

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

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

Copyimport 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:

Copy# 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:

Copyfor x in sorted(list):
    print x

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

🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › sort string in python
Sort string in Python | Working of Sort string in Python | Examples
April 12, 2023 - The sort string in python accepts a string as a parameter, then each character of the string compare based on the ASCII value of the characters and then return the list of sorted characters of the string. ... Example for sorting the string using python built in sorted() method.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
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 ...
🌐
Note.nkmk.me
note.nkmk.me › home › python
Sort a List, String, Tuple in Python: sort, sorted | note.nkmk.me
August 17, 2023 - In Python, there are two ways to sort a list in ascending or descending order: the sort() method and the built-in sorted() function. To sort a string (str) or tuple, use sorted(). Sort a list using th ...
🌐
Note.nkmk.me
note.nkmk.me › home › python
Sort a List of Numeric Strings in Python | note.nkmk.me
January 31, 2024 - Note that the following sample code uses sorted(), but sort() works similarly. l = ['10', '01', '05'] print(sorted(l)) # ['01', '05', '10'] ... A list of numeric strings that are not zero-padded is sorted alphabetically, not numerically.
🌐
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)
🌐
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.
🌐
Python Guides
pythonguides.com › sort-a-string-in-python
How To Sort A String In Python?
March 19, 2025 - This can be particularly useful when you need to rearrange characters in a specific order, such as sorting alphabetically or in reverse order · string = "California" sorted_string = ''.join(sorted(string)) print( sorted_string) ...
🌐
Python Tutorial
pythontutorial.net › home › python basics › python sort list
To sort a list, you use the sort() method - Python Tutorial
March 26, 2025 - list.sort(reverse=True)Code language: Python (python) If a list contains strings, the sort() method sorts the string elements alphabetically. The following example uses the sort() method to sort the elements in the guests list alphabetically:
🌐
Intellipaat
intellipaat.com › home › blog › how to sort a string in python?
How to Sort a String in Python? - Intellipaat
February 3, 2026 - The sorted string is returned and printed. In this specific example, the input string “intellipaat” is sorted to “aaeiillnptt”. Learn what the functools module is in Python and how ...