### 8.3 Comparison of Terms

These built-in predicates are meta-logical. They treat uninstantiated variables as objects with values that may be compared, and they never instantiate those variables. They should not be used when what you really want is arithmetic comparison (see Arithmetic) or unification.

The predicates make reference to a standard total ordering of terms, which is as follows:

• Variables, by age (oldest first—the order is not related to the names of variables).
• Floats, in numeric order (e.g. -1.0 is put before 1.0).
• Integers, in numeric order (e.g. -1 is put before 1).
• Atoms, in alphabetical (i.e. character code) order.
• Compound terms, ordered first by arity, then by the name of the principal functor, then by age for mutables and by the arguments in left-to-right order for other terms. Recall that lists are equivalent to compound terms with principal functor `./2`.

For example, here is a list of terms in standard order:

```     [ X, -1.0, -9, 1, fie, foe, X = Y, foe(0,2), fie(1,1,1) ]
```
Please note: the standard order is only well-defined for finite (acyclic) terms. There are infinite (cyclic) terms for which no order relation holds. Furthermore, blocking goals (see Procedural) on variables or modifying their attributes (see Attributes) does not preserve their order.

These are the basic predicates for comparison of arbitrary terms:

Term1` == `Term2 ISO
The terms currently instantiating Term1 and Term2 are literally identical (in particular, variables in equivalent positions in the two terms must be identical). For example, the query
```          | ?- X == Y.
```

fails (answers `no') because X and Y are distinct uninstantiated variables. However, the query

```          | ?- X = Y, X == Y.
```

succeeds because the first goal unifies the two variables (see Misc Pred).

Term1` \== `Term2 ISO

The terms currently instantiating Term1 and Term2 are not literally identical.

Term1` @< `Term2 ISO
The term Term1 is before the term Term2 in the standard order.
Term1` @> `Term2 ISO
The term Term1 is after the term Term2 in the standard order.
Term1` @=< `Term2 ISO
The term Term1 is not after the term Term2 in the standard order.
Term1` @>= `Term2 ISO
The term Term1 is not before the term Term2 in the standard order.

Some further predicates involving comparison of terms are:

`?=(`?X`,`?Y`)`
X and Y are either syntactically identical or syntactically non-unifiable.
`compare(`?Op`,`?Term1`,`?Term2`)`
The result of comparing terms Term1 and Term2 is Op, where the possible values for Op are:
`=`
if Term1 is identical to Term2,
`<`
if Term1 is before Term2 in the standard order,
`>`
if Term1 is after Term2 in the standard order.

Thus `compare(=,Term1,Term2)` is equivalent to `Term1 == Term2`.

`sort(`+List1`,`?List2`)`
The elements of the list List1 are sorted into the standard order (see Term Compare) and any identical elements are merged, yielding the list List2. (The time and space complexity of this operation is at worst O(N lg N) where N is the length of List1.)
`keysort(`+List1`,`?List2`)`
The list List1 must consist of pairs of the form Key-Value. These items are sorted into order according to the value of Key, yielding the list List2. No merging takes place. This predicate is stable, i.e. if `K-A` occurs before `K-B` in the input, then `K-A` will occur before `K-B` in the output. (The time and space complexity of this operation is at worst O(N lg N) where N is the length of List1.)