You are given a list of filename strings (e.g., "file10.txt", "file2.txt", "10file.txt"). Implement a custom sort for these strings using a comparator with the following rules:
-
Digit-first at the first differing position
: When comparing two strings from left to right, if at the first position where they differ one string has a digit (
0-9
) and the other has a non-digit, the digit one should come first.
-
Numeric comparison for digit runs
: Treat any
maximal consecutive digit substring
as a single number and compare by
numeric value
(so
"file10.txt"
comes after
"file2.txt"
).
-
Normal character order for non-digit runs
: Compare non-digit parts in standard character/lexicographic order.
Return the filenames sorted according to these rules.
Follow-ups:
-
Leading zeros
: How should
"file01.txt"
and
"file1.txt"
be ordered if their numeric values are equal? Implement your chosen tie-break rule.
-
Performance
: If there are very many filenames, repeatedly parsing digit runs inside the comparator can be slow. How would you optimize the sorting implementation?