Module natcmp

Natural string comparison

This module provides functions for easily sorting and comparing strings. For implementing natural comparison methods in structs, see the NaturalComparable and NaturalPathComparable templates, or the compareNatural and comparePathsNatural functions for a more manual implementation.

For sorting ranges of strings, see the compareNaturalSort and comparePathsNaturalSort functions.

Functions

NameDescription
compareNatural(a, b)Compares two strings in a way that is natural to humans. Integers come before non-integers, and integers are compared as if they were numbers instead of strings of characters. Best used in opCmp overloads.
compareNaturalSort(a, b)Natural string comparison function for use with phobos's sorting algorithm
comparePathsNatural(pathA, pathB)Compares path strings naturally. Comparing paths naturally requires path separators to be treated specially. Intended for usage in opCmp overloads.
comparePathsNaturalSort(a, b)Path comparison function for use with phobos's sorting algorithm

Templates

NameDescription
NaturalComparableAutomatically generates natural-comparing opCmp, opEquals and toHash methods for a particular property or method. Methods must be @safe, nothrow, and const.
NaturalPathComparableAutomatically generates natural-path-comparing opCmp, opEquals and toHash methods for a particular property or method. Methods must be @safe, nothrow, and const.