compare_natural
Compares two strings in natural order: runs of ASCII digits are compared by their numeric
value, everything else character by character. So "frame 2" comes before "frame 10",
where plain text order puts "frame 10" first.
Letters compare by code point, so case matters ("B" before "a"): lowercase both sides
first for a case-insensitive order. Numbers of any length are compared exactly, without
converting them to an integer, and two numbers of equal value but different zero padding
("a1" and "a01") are ordered by padding, fewest zeros first, so only identical strings
compare as equal.
Import
Section titled “Import”Cargo feature string (enabled by default). To compile only this module:
or in Cargo.toml:
Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
a | &str | The first string. |
b | &str | The second string. |
Returns
Section titled “Returns”Ordering — How a orders against b. Usable as a comparator: names.sort_by(|a, b| compare_natural(a, b)).
