Skip to content

Instantly share code, notes, and snippets.

@loicteixeira
Created April 19, 2022 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loicteixeira/37d568ba5527d45d2532b8bc896f213b to your computer and use it in GitHub Desktop.
Save loicteixeira/37d568ba5527d45d2532b8bc896f213b to your computer and use it in GitHub Desktop.
Python alphanumeric/natural sort
import re
import unicodedata
# Case insensitivity
sorted(["Hola", "hello"])
# => ['Hola', 'hello']
sorted(
["Hola", "hello"],
key=str.lower,
)
# => ['hello', 'Hola']
# Accents insensitivity
sorted(["élodie", "francis"])
# => ['francis', 'élodie']
sorted(
["élodie", "francis"],
key=lambda item: unicodedata.normalize("NFKD", item).encode("ASCII", "ignore"),
)
# => ['élodie', 'francis']
# Alphanumeric
sorted(["file1", "file11", "file100", "file25", "file2"])
# => ['file1', 'file100', 'file11', 'file2', 'file25']
sorted(
["file1", "file11", "file100", "file25", "file2"],
key=lambda item: [int(part) if part.isdigit() else part for part in re.split(r"(\d+)", item)],
)
# => ['file1', 'file2', 'file11', 'file25', 'file100']
# Everything together
sorted(["file1", "FILE11", "file100", "file25", "file2", "photo_château_fort_2", "photo_chateau_FORT_1"])
# => ['FILE11', 'file1', 'file100', 'file2', 'file25', 'photo_chateau_FORT_1', 'photo_château_fort_2']
sorted(
["file1", "FILE11", "file100", "file25", "file2", "photo_château_fort_2", "photo_chateau_FORT_1"],
key=lambda item: [
int(part) if part.isdigit() else unicodedata.normalize("NFKD", item).encode("ASCII", "ignore").lower()
for part in re.split(r"(\d+)", item)
],
)
# => ['file1', 'file100', 'FILE11', 'file2', 'file25', 'photo_chateau_FORT_1', 'photo_château_fort_2']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment