#lang racket (define (natural-sort l) (define (listkeys s) (define s* (string-normalize-spaces (string-foldcase s))) (for/list ([x (regexp-match* #px"\\d+" s* #:gap-select? #t)] [i (in-naturals)]) (if (odd? i) (string->number x) x))) (sort l listkeys #:cache-keys? #t)) (natural-sort (shuffle '("foo9.txt" "foo10.txt" "x9y99" "x9y100" "x10y0" "x z" "x y"))) ;; => '("foo9.txt" "foo10.txt" "x9y99" "x9y100" "x10y0" "x y" "x z")