#file-sorting #sorting #path #file-path #alphanumeric #file

no-std alphanumeric-sort

This crate can help you sort order for files and folders whose names contain numerals

30 stable releases

1.5.3 Nov 26, 2023
1.5.1 Mar 22, 2023
1.4.4 Mar 5, 2022
1.4.3 Apr 21, 2021
1.0.6 Nov 13, 2018

#127 in Algorithms

Download history 5971/week @ 2024-02-04 5427/week @ 2024-02-11 5719/week @ 2024-02-18 5708/week @ 2024-02-25 7504/week @ 2024-03-03 6787/week @ 2024-03-10 6122/week @ 2024-03-17 6282/week @ 2024-03-24 7023/week @ 2024-03-31 6617/week @ 2024-04-07 6862/week @ 2024-04-14 6991/week @ 2024-04-21 6527/week @ 2024-04-28 5392/week @ 2024-05-05 10432/week @ 2024-05-12 7860/week @ 2024-05-19

30,646 downloads per month
Used in 40 crates (23 directly)

MIT license

30KB
643 lines

Alphanumeric Sort

CI

This crate can help you sort order for files and folders whose names contain numerals.

Motives and Examples

With the Rust native sort method, strings and paths are arranged into lexicographical order. In some cases, it is not so intuitive. For example, there are screen snap shots named by shot-%N like shot-2, shot-1, shot-11. After a lexicographical sorting, they will be ordered into shot-1, shot-11, shot-2. However, we would prefer shot-1, shot-2, shot-11 mostly.

let mut names = ["shot-2", "shot-1", "shot-11"];

names.sort();

assert_eq!(["shot-1", "shot-11", "shot-2"], names);

Thus, in this kind of case, an alphanumeric sort might come in handy.

let mut names = ["shot-2", "shot-1", "shot-11"];

alphanumeric_sort::sort_str_slice(&mut names);

assert_eq!(["shot-1", "shot-2", "shot-11"], names);
use std::path::Path;

let mut paths = [Path::new("shot-2"), Path::new("shot-1"), Path::new("shot-11")];

alphanumeric_sort::sort_path_slice(&mut paths);

assert_eq!([Path::new("shot-1"), Path::new("shot-2"), Path::new("shot-11")], paths);

About the compare_* Functions and the sort_* Functions

To sort a slice, the code can also be written like,

use std::path::Path;

let mut paths = [Path::new("shot-2"), Path::new("shot-1"), Path::new("shot-11")];

paths.sort_by(|a, b| alphanumeric_sort::compare_path(a, b));

assert_eq!([Path::new("shot-1"), Path::new("shot-2"), Path::new("shot-11")], paths);

But it is not recommended because the compare_* functions try to convert data (e.g Path, CStr) to &str every time in its execution and thus they are slower than the sort_* functions when sorting a slice.

Version 1.3 to 1.4

No breaking change in API is made, though the order has some changes.

  • "0001" is greater than "001" instead of being equal.
  • "" is greater than "1" instead of being less. "第1章" is still less than "第1-2章", even though "" is greater than "-".

No Std

Disable the default features to compile this crate without std.

[dependencies.alphanumeric-sort]
version = "*"
default-features = false

Benchmark

cargo bench

Crates.io

https://crates.io/crates/alphanumeric-sort

Documentation

https://docs.rs/alphanumeric-sort

License

MIT

No runtime deps