pub fn natural_cmp(a: &str, b: &str) -> OrderingExpand description
Compare two human-facing strings the way a person reads them.
A run of ASCII digits compares by value, so item-2 precedes item-10 instead of
following it. Everything else compares by Unicode scalar, case-insensitively for ASCII
letters first so that Alpha and alpha sort together, and case-sensitively after, so
that two strings differing only in case still have a fixed order rather than comparing
equal and letting the caller’s input order decide.
Separators are not normalised. a-b and a_b are different strings and stay different:
folding them together would make two distinct identities compare equal, which is exactly
the tie this order exists to break.
use majordomus_cli::order::natural_cmp;
use std::cmp::Ordering;
assert_eq!(natural_cmp("item-2", "item-10"), Ordering::Less);
assert_eq!(natural_cmp("item-002", "item-2"), Ordering::Less);
assert_eq!(natural_cmp("Alpha", "beta"), Ordering::Less);