Resolved

Python file sorting breaks numerical series order with part-1 part-10 part-2

Python
File Handling
Sorting
Regex
String Processing
Backend
Automation

Default string sorting in Python causes incorrect video series ordering (part-10 before part-2) | Fixed with regex-based numerical sort keys for part-X-of-Y filename patterns

Issue

The default sorting behavior in Python caused incorrect ordering when processing video files that follow a series pattern, such as part-1-of-10, part-2-of-10, etc. Due to string-based comparison, files were sorted alphabetically rather than numerically. For example, part-10-of-16.md appeared before part-2-of-16.md, disrupting the intended sequential order. This issue affected workflows relying on natural progression, like video series playback or processing pipelines.

Culprit

The root cause was Python’s default lexicographic string comparison, which evaluates numbers as strings. For instance, comparing '10' and '2' treats them as text, leading to incorrect numerical ordering. This problem occurred in systems where video files were sorted without considering their numerical structure, resulting in misaligned processing sequences.

Solution

To address this, a custom sorting mechanism was implemented:

  1. Pattern Detection: A regex-based approach was used to extract numerical part information (e.g., part-5-of-10) from filenames. This involved identifying part numbers and total parts while accommodating variations like zero-padding, case sensitivity, and single/double-digit formats.
  2. Numerical Sorting: A custom sort key was introduced to replace the default string comparison. The key extracted part numbers as integers, ensuring correct numerical ordering (e.g., 1, 2, 3…10 instead of '1', '10', '2'). Series were grouped by name, and secondary sorting criteria (e.g., video format) were preserved.
  3. Robustness: The solution supports diverse filename formats, including:
    • Zero-padded numbers (e.g., part-03-of-16).
    • Non-padded numbers (e.g., part-5-of-10).
    • Case variations (e.g., PART-2-OF-8). Non-series files were handled separately, ensuring they did not interfere with the sorting of series content.

This approach eliminated the misordering issue while maintaining compatibility with existing workflows and preserving the logical grouping of series content.