# Understanding issorted's lt keyword

**URL:** https://discourse.julialang.org/t/understanding-issorteds-lt-keyword/60918
**Category:** New to Julia
**Created:** [May 10, 2021, 9:34pm UTC](https://discourse.julialang.org/t/understanding-issorteds-lt-keyword/60918 "2021-05-10T21:34:36Z")
**Posts on this page:** 1
**Showing post:** 55

<div class="post-metadata">

### Author: ![goretkin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goretkin/32/167_2.png) [@goretkin](https://discourse.julialang.org/u/goretkin)
#### Post date: [May 12, 2021, 6:05pm UTC](https://discourse.julialang.org/t/understanding-issorteds-lt-keyword/60918/55 "2021-05-12T18:05:37Z")

</div>

> [@gustaphe](#):
>
> The way I see it, _should_ -wise: `issorted` should simply check whether `lt` holds for each consecutive pair.

That means that the default `lt = isless` would be wrong, since it would give `issorted([1, 1]) == false`.

You could then suggest the default be `lt = <=`, but note that the fallback for `<=` is defined in terms of `<`:

> <https://github.com/JuliaLang/julia/blob/d6c092d6d73be9b41b5b8fef30d882fdb63a8d20/base/operators.jl#L401>

And `<` is defined in terms of `isless`:

> <https://github.com/JuliaLang/julia/blob/d6c092d6d73be9b41b5b8fef30d882fdb63a8d20/base/operators.jl#L352>

If you simply define `isless` and `==` (EDIT: not `isequal`), you get the desired (probably) definitions of the other operators. I’d say that this is nothing more than a design choice, but it’s one that works pretty well in my opinion. And indeed you can write `issorted` in terms of `isless`: instead of checking that every consecutive element satisfies a property, you instead check that no consecutive element violates a property.

I understand that this extra negation and swapping of the arguments is not the first definition of `issorted` that some people are thinking of. But the definition that _is_ in `Base` is self-consistent. That’s not to say that this can’t be better documented.

I’ll add that there might be cases where it’s better to define `isless` on your type (possible a wrapper type) than it is to pass in a function for `lt`. I asked about these two choices once upon a time: [Designing APIs. defining new methods versus passing in functions](https://discourse.julialang.org/t/designing-apis-defining-new-methods-versus-passing-in-functions/18699)

> [@Designing APIs. defining new methods versus passing in functions](https://discourse.julialang.org/t/designing-apis-defining-new-methods-versus-passing-in-functions/18699/3):
>
> Once you define `isless` on your type (your thing), then you can use `min` , `minimum` , `sort` , you can use the lexicographic ordering induced by `isless` on `NTuple{Thing}` , and so forth. (So, here’s a benefit of the second API)

---

_[View the full topic](https://discourse.julialang.org/t/understanding-issorteds-lt-keyword/60918)._
