2 releases

0.1.1 Oct 4, 2024
0.1.0 Oct 4, 2024

#8 in #en

MIT/Apache

12KB
226 lines

Crates.io docs.rs

A #![no_std] implementation of natural sort order

Example

use natural_sort_rs::{Natural, NaturalSort};

fn main() {
    let mut files = ["file2.txt", "file11.txt", "file1.txt"];
    files.sort();
    assert_eq!(files, ["file1.txt", "file11.txt", "file2.txt"]);

    assert!(Natural::str("file0002.txt") > Natural::str("file1B.txt"));
    assert!(Natural::str("file0002.txt") < Natural::str("file11.txt"));

    let mut files = [
        "file1.txt",
        "file1B.txt",
        "file00.txt",
        "file11.txt",
        "file0002.txt",
    ];

    files.natural_sort::<str>();


    // Here, "file11.txt" comes last because `natural_sort` saw that there was a
    // number inside the string, and did a numerical, rather than lexical,
    // comparison.
    assert_eq!(
        files,
        [
            "file00.txt",
            "file1.txt",
            "file1B.txt",
            "file0002.txt",
            "file11.txt"
        ]
    );
}

No runtime deps

Features