# What comparison operators I need to define?

**URL:** <https://discourse.julialang.org/t/what-comparison-operators-i-need-to-define/4895>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 17, 2017, 9:02am UTC](https://discourse.julialang.org/t/what-comparison-operators-i-need-to-define/4895 "2017-07-17T09:02:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [July 17, 2017, 9:02am UTC](https://discourse.julialang.org/t/what-comparison-operators-i-need-to-define/4895/1 "2017-07-17T09:02:36Z")

</div>

I find it inconsistent that to define equality, I should define `Base.:(==)` instead of `Base.isequal`, but to define inequality, I should define `Base.isless` instead of `Base.:(<)`. What’s the rationale for this?

In general, if I define a custom `struct` and I define a custom equality / comparison operator, what are the methods that I should define to obtain a consistent behavior?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [July 17, 2017, 9:15am UTC](https://discourse.julialang.org/t/what-comparison-operators-i-need-to-define/4895/2 "2017-07-17T09:15:30Z")

</div>

> [@e3c6](#):
>
> to define inequality, I should define `Base.isless` instead of `Base.:(<)`

In `operators.jl`, the generic `isless` method for `Real` relies on `<`, so you should actually define `<` for a custom type, not `isless`, which isn’t necessary for `Real` subtypes (if that’s what you’re interested in).

> [@e3c6](#):
>
> In general, if I define a custom struct and I define a custom equality / comparison operator, what are the methods that I should define to obtain a consistent behavior?

For basic usage, `==`, `<`, `<=` should suffice

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [July 17, 2017, 9:16am UTC](https://discourse.julialang.org/t/what-comparison-operators-i-need-to-define/4895/3 "2017-07-17T09:16:28Z")

</div>

Well I am confused about this. Are there guidelines written somewhere, of when and which operators I should define for consistent behavior? I suppose I need to know whether `isless` is defined in terms of `<` or the other way around, for each class of types… and so on. Of course I could just define them all, but that is not efficient 😛

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [July 17, 2017, 2:49pm UTC](https://discourse.julialang.org/t/what-comparison-operators-i-need-to-define/4895/4 "2017-07-17T14:49:56Z")

</div>

> [@giordano](#):
>
> For basic usage, ==, \<, \<= should suffice

But if you redefine `==`, don’t you also need to (re)define `hash`?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [July 17, 2017, 2:59pm UTC](https://discourse.julialang.org/t/what-comparison-operators-i-need-to-define/4895/5 "2017-07-17T14:59:06Z")

</div>

> [@anon94023334](#):
>
> But if you redefine `==`, don’t you also need to (re)define `hash`?

Yes, but I’m not sure this entails basic usage.
