Explain Codes LogoExplain Codes Logo

Is there a built in function for string natural sort?

python
natural-sort
file-paths
custom-order
Anton ShumikhinbyAnton Shumikhin·Oct 29, 2024
TLDR

The go-to Python library for natural string sorting is natsort. By running pip install natsort, you can then apply natsorted:

from natsort import natsorted natural_sorted_list = natsorted(['item1', 'item12', 'item2']) print(natural_sorted_list) # ['item1', 'item2', 'item12']

This generates the result you'd want, treating embedded numbers as integers rather than individual characters.

Know your toolbox: natsort

The natsort library broadens the horizons of string sorting in Python. This library helps sort strings with embedded digits in a more human-friendly way: logically.

File paths and os_sorted

When dealing with file paths or trying to replicate the sort mechanism of Windows Explorer, the os_sorted function packs the punch:

from natsort import os_sorted sorted_filepaths = os_sorted(['/path/to/File1', '/path/to/File12', '/path/to/File2']) print(sorted_filepaths) # ['/path/to/File1', '/path/to/File2', '/path/to/File12'] # Even Python likes to keep things tidy, just like your desktop!

It naturally sorts file paths to give outputs that align with common file explorers.

Cooking up a custom order with key generators

natsort is not rigid. It offers key generator functions like natsort_keygen or os_sort_keygen that let you create custom sorting keys, just like your own secret recipe:

from natsort import natsort_keygen keygen = natsort_keygen() personalized_order = sorted(['a', 'b', '11', '2'], key=keygen) # Flavor? Sorted. Jazz? Sorted. Order? You bet, sorted!

No-libs natural sorting for purists

Are you a fan of keeping things simple and library-free? Behold, we can implement a natural sort key function using Python's regular expressions:

import re def make_natural(s): return [int(text) if text.isdigit() else text.lower() for text in re.split('(\d+)', s)] sorted_list_without_lib = sorted(['item1', 'item12', 'item2'], key=make_natural) # Libraries? Where we're going we don't need libraries.

Unraveling the 'how it works' mystery

The natsort library follows a clear-cut algorithmic pathway. It's useful to understand the logic behind it, which involves:

  • Splitting strings into parts of digits and non-digits.
  • Converting digit parts to integers for logical comparison.
  • Reassembling these parts in the sorted order.

This three-step waltz helps natsort to cleanly breakdown and sort your strings.

Brushing up on the fine print

Remember to consult the natsort API for precise application and understanding of its abilities and constraints. For instance, locale-aware sorting can be a tough nut to crack. Paying attention to these details makes for clean, efficient, and error-free sorting.