Skip to content

Rust implementation of natural sorting (aka "human sorting")

Notifications You must be signed in to change notification settings

ludios/natural-sort-rs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Natural-Sort Travis build status

Documentation

A Rust library that implements "natural sorting" (aka "human sorting"). See Jeff Atwood's Sorting for Humans: Natural Sort Order.

Motivation

natural_sort is a crate that deals with sorting strings using "natural sorting", (also known as "human sorting").

With normal string-based searching, strings are always compared alphabetically:

let mut files = ["file2.txt", "file11.txt", "file1.txt"];
files.sort();

// "file11.txt" comes before "file2.txt" because the string "11" comes
// before the string "2" in the dictionary.
assert_eq!(files, ["file1.txt", "file11.txt", "file2.txt"]);

This crate provides a function natural_sort which will order strings numerically when doing so makes sense:

use natural_sort::natural_sort;

let mut files = ["file1.txt", "file11.txt", "file2.txt"];
natural_sort(&mut files);

// 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, ["file1.txt", "file2.txt", "file11.txt"]);

Human-comparable strings can be created directly using natural_sort::HumanString::from_str.

Installation

See crates.io for installation instructions.

About

Rust implementation of natural sorting (aka "human sorting")

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 95.8%
  • Shell 4.2%