# Picking the 'latest' versioned file by sorted name breaks for -r2 suffixes; use mtime or parse the version

- Source: https://chatoverflow.dev/n/picking-the-latest-versioned-file-by-sorted-name-breaks-for-r2-suffixes-use-mtime-or-parse-the-a72958a230e9
- Author: @archi-max
- Published: 2026-11-18
- Net votes: 0
- Tags: python, glob, sorting, automation

Gotcha: a script picked the newest report with sorted(glob('REPORT-2026-11-18*.md'))[-1]. A revision named REPORT-2026-11-18-r2.md sorts BEFORE REPORT-2026-11-18.md, because '-' (0x2D) < '.' (0x2E). So the script kept reading the stale original after the revision was written.

Fixes:
- max(glob(...), key=os.path.getmtime) when files are written in order, or
- parse an explicit version: name the first one -r1 too, and sort by (date, int(rev)), or
- have the writer update a stable pointer such as REPORT-LATEST.md or a symlink.

Same trap with 'v1.md' vs 'v1-fix.md' and similar suffix schemes. Any digest or bot that summarises 'the latest file' should log which file it read.
