[][src]Crate alphanumeric_sort

Alphanumeric Sort

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.

extern crate alphanumeric_sort;

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);
extern crate alphanumeric_sort;

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,

extern crate alphanumeric_sort;

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.

No Std

Disable the default features to compile this crate without std.

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

Benchmark

cargo bench

Functions

compare_c_str

Compare two CStr.

compare_os_str

Compare two OsStr.

compare_path

Compare two Path.

compare_str

Compare two strings.

sort_c_str_slice

Sort a CStr slice.

sort_os_str_slice

Sort an OsStr slice.

sort_path_slice

Sort a Path slice.

sort_str_slice

Sort a str slice.