Method

# Operator_Compare

<div class="rst-class">

forsearch

</div>

Operator

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Used to overload the intrinsic comparison operators, providing custom functionality.

## Notes

If <span class="title-ref">Operator_Compare</span> is defined, it is called whenever any of the comparison operators are used to compare the `Self</api/language/self_keyword>` instance of the class with another instance. The following comparison operators are covered: `=</api/language/equals>`, `<</api/language/operators/comparison/less_than>`, `\></api/language/operators/comparison/greater_than>`, `\<=</api/language/operators/comparison/less_than_or_equal>`, `\>=</api/language/operators/comparison/greater_than_or_equal>`, `<></api/language/operators/comparison/not_equal>`.

Operator_Compare returns an `integer</api/data_types/integer>` whose meaning is as follows: \<0 means that `Self</api/language/self_keyword>` is less than the passed parameter, 0 means that `Self</api/language/self_keyword>` is equal to the passed parameter, and \>0 means that `Self</api/language/self_keyword>` is greater than the passed parameter.

## Sample code

This example defines a comparison between two Vectors which have been declared with `integer</api/data_types/integer>` properties x and y (See `Operator Add</api/language/operators/operator_overloads/operator_add>` for a description of the Vector class). The function compares the square lengths of the vectors and then reports on which one is longer.

``` xojo
Function Operator_Compare(rhs As Vector) As Integer
  Var a, b As Integer
  a = Self.x ^ 2 + Self.y ^ 2
  b = rhs.x ^ 2 + rhs.y ^ 2
  If a > b Then Return 1
  If a = b Then Return 0
  If a < b Then Return -1
End Function
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`=</api/language/equals>`, `<</api/language/operators/comparison/less_than>`, `\></api/language/operators/comparison/greater_than>`, `\<=</api/language/operators/comparison/less_than_or_equal>`, `\>=</api/language/operators/comparison/greater_than_or_equal>`, `<></api/language/operators/comparison/not_equal>` operators.

</div>
