# Human sorting, hardened

DevFeed: [Human sorting, hardened](<https://devfeed.tech/articles/human-sorting-hardened-153606.md>)

Original publisher: [Read original article](<https://nedbatchelder.com/blog/202611/human_sorting_hardened>)

Author: Ned Batchelder

Published: 2026-11-25T12:46:25Z

Content type: article

Language: en

Sources: [Ned Batchelder's blog](<https://devfeed.tech/sources/ned-batchelder-s-blog.md>)

Topics: [Sorting](<https://devfeed.tech/topics/sorting.md>), [Regular expression](<https://devfeed.tech/topics/regular-expression.md>)

Tags: [ai](<https://devfeed.tech/tags/ai.md>), [bug-reports](<https://devfeed.tech/tags/bug-reports.md>), [development](<https://devfeed.tech/tags/development.md>), [edge](<https://devfeed.tech/tags/edge.md>), [gems](<https://devfeed.tech/tags/gems.md>), [python](<https://devfeed.tech/tags/python.md>), [sorting](<https://devfeed.tech/tags/sorting.md>)

## AI overview

The human sort function failed on a digit run longer than 4,300 characters: Python's int conversion raises ValueError at its default limit, causing the code to treat the digits as text and later compare a string with an integer. The fix converts only the digit spans identified by re.split's alternating word/number pattern, replacing an overlong number with float("inf"). The author rejects checking parts with isdigit(), since it disagrees with \d for some Unicode characters and can misclassify text. The excerpt presents this as a small fix to a rare edge case, while noting that further edge cases may remain.

## Source excerpt

"Human sorting" is sorting strings (usually file names) so that any numbers in the names come out in the right order. For example, "file10.txt" should come after "file2.txt", but regular sorting will put them in the wrong order. I've written twice before about the function to do this (Human sorting and Human sorting improved), but there's more to say about it. One effect of AI these days is bug reports for obscure edge cases. As a maintainer, it's been a challenge first to read through them all, and then to decide which ones deserve a fix. It's tempting to fix every little thing no matter how unlikely it is to happen in the real world. But every fix costs time, energy and focus. Every fix comes with risks in the form of new bugs or changed behavior. So some long-tail issues should be marked as "won't fix." But this one intrigued me: if a file name includes a numeric component with 4301 digits, then the human sorting function fails. Buh? Before we get to the current problem, here's the original "buggy" code, slightly simplified: def human_key(s: str) -> tuple[list[str | int], str]: """Turn a string into a sortable value that works how humans expect. "z23A" -> (["z", 23, "A"], "z23A") """ def try_int(s: str) -> str | int: """If `s` is a number, return an int, else `s` unchanged.""" try: return int(s) except ValueError: return s parts = re.split(r"(\d+)", s) split_key = [try_int(c) for c in parts] return (split_key, s) def human_sorted(strings: Iterable[str]) -> list[str]: """Sort the given iterable of strings the way that humans expect.""" return sorted(strings, key=human_key) It splits the string into parts alternating between runs of digits and runs of non-digits. It uses try_int on all of them, which turns the digits into ints and leaves the non-digits as strings. That gives us a list of words and numbers that can be compared to do the sort we need. The problem now is with that 4301-digit run. Since 2022, Python's string-to-integer conversion has had a limit on the