compareNatural

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. Intended for usage in opCmp overloads.

  1. int compareNatural(dchar[] a, dchar[] b)
    nothrow @safe pure
    int
    compareNatural
    (
    inout dchar[] a
    ,
    inout dchar[] b
    )
    out (result) { assert (result <= 1, "Result too large"); assert (result >= -1, "Result too small"); }
  2. int compareNatural(char[] a, char[] b)
  3. int compareNatural(wchar[] a, wchar[] b)

Return Value

Type: int

-1 if a comes before b, 0 if a and b are equal, 1 if a comes after b

Examples

struct someStruct {
    dstring someText;
    int opCmp(someStruct b) {
         return compareNatural(this.someText, b.someText);
    }
}

Meta