Python: Sort unsorted strings using natural sort
38. Natural Sort for Strings
Write a Python program to sort unsorted strings using natural sort.
Natural sort order is an ordering of strings in alphabetical order, except that multi-digit numbers are treated atomically, i.e., as if they were a single character. Natural sort order has been promoted as being more human-friendly ("natural") than the machine-oriented pure alphabetical order.
For example, in alphabetical sorting "z11" would be sorted before "z2" because "1" is sorted as smaller than "2", while in natural sorting "z2" is sorted before "z11" because "2" is sorted as smaller than "11".
Alphabetical sorting:
z11
z2
Natural sorting:
z2
z11
Sample Solution:
Python Code:
#Ref.https://bit.ly/3a657IZ
from __future__ import annotations
import re
def natural_sort(input_list: list[str]) -> list[str]:
def alphanum_key(key):
return [int(s) if s.isdigit() else s.lower() for s in re.split("([0-9]+)", key)]
return sorted(input_list, key=alphanum_key)
strs = ['2 ft 7 in', '1 ft 5 in', '10 ft 2 in', '2 ft 11 in', '7 ft 6 in']
print("\nOriginal list:")
print(strs)
natural_sort(strs)
print("Sorted order is:", strs)
strs = ['1 ft 5 in', '10 ft 2 in', '2 ft 11 in', '2 ft 7 in', '7 ft 6 in']
print("\nOriginal list:")
print(strs)
natural_sort(strs)
print("Sorted order is:", strs)
strs = ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9']
print("\nOriginal list:")
print(strs)
natural_sort(strs)
print("Sorted order is:", strs)
strs = ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9']
print("\nOriginal list:")
print(strs)
natural_sort(strs)
print("Sorted order is:", strs)
Sample Output:
Original list: ['2 ft 7 in', '1 ft 5 in', '10 ft 2 in', '2 ft 11 in', '7 ft 6 in'] Sorted order is: ['2 ft 7 in', '1 ft 5 in', '10 ft 2 in', '2 ft 11 in', '7 ft 6 in'] Original list: ['1 ft 5 in', '10 ft 2 in', '2 ft 11 in', '2 ft 7 in', '7 ft 6 in'] Sorted order is: ['1 ft 5 in', '10 ft 2 in', '2 ft 11 in', '2 ft 7 in', '7 ft 6 in'] Original list: ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9'] Sorted order is: ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9'] Original list: ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9'] Sorted order is: ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9']
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to sort a list of strings using natural sort order where numeric parts are compared as numbers.
- Write a Python script to implement natural sort on filenames that include numbers and then print the sorted order.
- Write a Python program to perform natural sorting on a list of alphanumeric strings and compare the result with lexicographical sorting.
- Write a Python function to implement natural sort and then output the list sorted in human-friendly order.
Python Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Python program to sort unsorted numbers using Odd Even Transposition Parallel sort.
Next: Write a Python program to sort unsorted numbers using Merge-insertion sort.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.