Files
zenml/venv/lib/python3.9/site-packages/graphql/pyutils/natural_compare.py
Christian Mantha 2ca0b9ef7c star
2026-03-02 19:10:52 -05:00

20 lines
478 B
Python

import re
from typing import Tuple
from itertools import cycle
__all__ = ["natural_comparison_key"]
_re_digits = re.compile(r"(\d+)")
def natural_comparison_key(key: str) -> Tuple:
"""Comparison key function for sorting strings by natural sort order.
See: https://en.wikipedia.org/wiki/Natural_sort_order
"""
return tuple(
(int(part), part) if is_digit else part
for part, is_digit in zip(_re_digits.split(key), cycle((False, True)))
)