compareNatural

compareNatural(x, y)

Try it yourself:


compareNatural(): compare text the way people expect

compareNatural(x, y) compares strings using natural ordering. Rather than comparing every character literally, it recognizes number runs, so “chapter 2” sorts before “chapter 10”.

Where it helps

Natural comparison is ideal for filenames, version-like labels, invoice identifiers, and numbered headings. A plain lexical order would place 10 before 2 because “1” precedes “2”; natural order treats the embedded digits as numbers.

Related comparisons

Use compareText() when text collation is the primary concern, and compare() for general values. Feed a comparator into sorting workflows with sort(). For exact identity rather than ordering, use equalText or equal.

Practical cautions

Natural ordering is a presentation rule, not a numeric parser. Leading zeros, punctuation, case, and locale can still matter. Normalize labels first when a reproducible cross-system order is required.

Try Compare Natural in Calcul.io

Start with one of the editable examples above, then replace its arguments with your own values. Keeping the function on a separate calculator line makes the input and result easy to compare. For a longer workflow, assign the result to a variable or reference that line in the next expression.

Check the shown signature before adding optional arguments, and use the related-function links to compare operations with similar purposes.

All functions