<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/human-sorting-hardened-u3powor3k" -->

---
title: Human sorting, hardened | daily.dev
description: Python's human sort function crashes on a file name containing a run of 4301 or more digits. Since 2022, int() raises ValueError for digit strings over 4300...
canonical: https://daily.dev/posts/human-sorting-hardened-u3powor3k
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Human sorting, hardened | daily.dev
og:description: Python's human sort function crashes on a file name containing a run of 4301 or more digits. Since 2022, int() raises ValueError for digit strings over 4300...
og:url: https://daily.dev/posts/human-sorting-hardened-u3powor3k
og:image: https://api.daily.dev/og/posts/U3PoWor3k.png
og:image:alt: Human sorting, hardened
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Human sorting, hardened

**[Planet Python](https://daily.dev/sources/planetpython)** · 5 min read · 0 upvotes · 0 comments

## Summary

Python's human sort function crashes on a file name containing a run of 4301 or more digits. Since 2022, int() raises ValueError for digit strings over 4300 characters. The old code used ValueError to tell words from numbers, so the over-long number stayed a string and caused a TypeError when compared to an int. Ned Batchelder's fix converts only the odd-numbered parts that re.split returns, and a number() helper returns float('inf') when conversion fails. He rejects an isdigit() check because it disagrees with \d on Unicode digits like '²'.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://nedbatchelder.com/blog/202611/human_sorting_hardened>

## Questions this post answers

### Why does my Python natural sort key raise TypeError comparing int and str on a very long digit string?

Since 2022, Python's int() raises ValueError for digit strings longer than 4300 characters. If your sort key uses try/except ValueError to separate numbers from words, the over-long number stays a string. Sorting then compares an int to a str and fails with TypeError: '<' not supported between instances of 'int' and 'str'.

_Developers maintaining Python utilities follow edge cases like this on daily.dev._

### How do I make a human sort key in Python robust against huge numbers and Unicode digits?

Split with re.split(r"(\d+)", s); parts alternate between words and digit runs, starting with a word. Convert only the odd-indexed parts using a number() helper that returns float('inf') when int() raises ValueError. Avoid checking isdigit(), because it disagrees with \d on characters like '²', which would mistake words for numbers.

_Engineers hardening string-handling code find more Python pitfalls on daily.dev._

## Similar posts on daily.dev

- [Plenty of IDs](https://daily.dev/posts/plenty-of-ids-os1ut1q59) · The Daily WTF · 1 upvotes · 0 comments
- [Python: fix SyntaxWarning: invalid octal escape sequence](https://daily.dev/posts/python-fix-syntaxwarning-invalid-octal-escape-sequence-5zjlu8hto) · Adam Johnson · 0 upvotes · 0 comments
- [DX improvements in Python 3.15](https://daily.dev/posts/dx-improvements-in-python-3-15-sq7j4fjy9) · Planet Python · 0 upvotes · 0 comments

---

Tags: [#python](https://daily.dev/tags/python), [#open-source](https://daily.dev/tags/open-source)

[View this post on daily.dev](https://daily.dev/posts/human-sorting-hardened-u3powor3k)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Human sorting, hardened","url":"https://daily.dev/posts/human-sorting-hardened-u3powor3k","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/human-sorting-hardened-u3powor3k"},"datePublished":"2026-11-25T13:48:33.526Z","dateModified":"2026-11-25T13:48:53.573Z","description":"Python's human sort function crashes on a file name containing a run of 4301 or more digits. Since 2022, int() raises ValueError for digit strings over 4300...","image":"https://media.daily.dev/image/upload/s--OHB84bZF--/f_auto/v1722860399/public/Placeholder%2010","thumbnailUrl":"https://media.daily.dev/image/upload/s--OHB84bZF--/f_auto/v1722860399/public/Placeholder%2010","isAccessibleForFree":true,"articleSection":"Planet Python","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Planet Python","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/eac3cc584cb043b5a242d13752bd3d0c","url":"https://daily.dev/sources/planetpython"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/human-sorting-hardened-u3powor3k","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"python,open-source","timeRequired":"PT5M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Planet Python","item":"https://daily.dev/sources/planetpython"},{"@type":"ListItem","position":3,"name":"Human sorting, hardened"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/human-sorting-hardened-u3powor3k#faq","mainEntity":[{"@type":"Question","name":"Why does my Python natural sort key raise TypeError comparing int and str on a very long digit string?","acceptedAnswer":{"@type":"Answer","text":"Since 2022, Python's int() raises ValueError for digit strings longer than 4300 characters. If your sort key uses try/except ValueError to separate numbers from words, the over-long number stays a string. Sorting then compares an int to a str and fails with TypeError: '\u003c' not supported between instances of 'int' and 'str'. Developers maintaining Python utilities follow edge cases like this on daily.dev."}},{"@type":"Question","name":"How do I make a human sort key in Python robust against huge numbers and Unicode digits?","acceptedAnswer":{"@type":"Answer","text":"Split with re.split(r\"(\\d+)\", s); parts alternate between words and digit runs, starting with a word. Convert only the odd-indexed parts using a number() helper that returns float('inf') when int() raises ValueError. Avoid checking isdigit(), because it disagrees with \\d on characters like '²', which would mistake words for numbers. Engineers hardening string-handling code find more Python pitfalls on daily.dev."}}]}
```

