# compare() Function & Examples

Use the compare() relational function in Calcul.io. Review its syntax, edit working examples, understand the result, and explore related math functions.

## compare

#### compare(x, y)

Try it yourself:

```calculio
compare(2, 3)
compare(3, 2)
compare(2, 2)
compare(5cm, 40mm)
compare(2, [1, 2, 3])
```

[equal](https://calcul.io/function/equal/index.md)

[unequal](https://calcul.io/function/unequal/index.md)

[smaller](https://calcul.io/function/smaller/index.md)

---

## compare(): order two values

**compare(x, y)** compares two values and returns a negative value when x is smaller, zero when they are equal, and a positive value when x is larger. This three-way result is useful in sorting rules and conditional calculations.

## Why not just subtract?

For ordinary finite numbers, subtraction may suggest the order, but it does not work for text, dates, units, or many structured values. compare() states the intended operation directly and avoids treating a difference as a Boolean answer.

## Choose the right comparator

For human-oriented numeric text such as “file2” before “file10”, use [compareNatural()](https://calcul.io/function/compareNatural/index.md). For text-specific ordering, see [compareText()](https://calcul.io/function/compareText/index.md). Use [equal](https://calcul.io/function/equal/index.md) when only equality matters, [larger](https://calcul.io/function/larger/index.md) for a Boolean greater-than test, and [sort()](https://calcul.io/function/sort/index.md) to order a collection.

## Consistency matters

Compare values of compatible types and keep one comparison rule throughout a sort. Mixed types, locale differences, and approximate floating-point values can produce surprising orderings; round or normalize data deliberately first.

[All functions](https://calcul.io/functions/index.md)
