# What should \`issorted\` return when an order isn't defined for the elements, but a comparison works?

**URL:** <https://discourse.julialang.org/t/what-should-issorted-return-when-an-order-isnt-defined-for-the-elements-but-a-comparison-works/101423>\
**Category:** General Usage\
**Tags:** sort\
**Created:** [July 10, 2023, 9:46am UTC](https://discourse.julialang.org/t/what-should-issorted-return-when-an-order-isnt-defined-for-the-elements-but-a-comparison-works/101423 "2023-07-10T09:46:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [July 10, 2023, 9:46am UTC](https://discourse.julialang.org/t/what-should-issorted-return-when-an-order-isnt-defined-for-the-elements-but-a-comparison-works/101423/1 "2023-07-10T09:46:28Z")

</div>

Currently, `issorted` returns `true` by default, even if the elements do not have a defined order. For example,

```julia
julia> issorted([NaN, NaN])
true

```

In this case, because `NaN` is a floating-point number, the comparison works, except we obtain

```julia
julia> NaN >= NaN || NaN < NaN
false

```

This reflects the fact that `NaN`s are unordered. Having `issorted` return true in this case doesn’t seem ideal. I wonder if there might be a better way to convey the message here?

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [July 10, 2023, 10:22am UTC](https://discourse.julialang.org/t/what-should-issorted-return-when-an-order-isnt-defined-for-the-elements-but-a-comparison-works/101423/2 "2023-07-10T10:22:17Z")

</div>

It appears

```julia
julia> isequal(NaN, NaN)
true

```

The docstring states

> Furthermore, isequal is linked with isless, and they work together to define a fixed total ordering, where exactly one of isequal(x, y), isless(x, y), or isless(y, x) must be true (and the other two false).

I suppose this is what sorting uses.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [July 10, 2023, 10:42am UTC](https://discourse.julialang.org/t/what-should-issorted-return-when-an-order-isnt-defined-for-the-elements-but-a-comparison-works/101423/3 "2023-07-10T10:42:21Z")

</div>

This is now better documented on `master`, see the [new `sort!` documentation](https://docs.julialang.org/en/v1.11-dev/base/sort/#Base.sort!).

In short, the sort functions don’t use `isequal`, by default they use the ordering defined by `isless`. Values `a` and `b` are considered “equivalent” (a generalization of “equal”) if `isless(a,b)` and `isless(b,a)` both return false. This is the case for `a=NaN`, `b=NaN` so you get `issorted([NaN, NaN]) == true`.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [July 10, 2023, 10:55am UTC](https://discourse.julialang.org/t/what-should-issorted-return-when-an-order-isnt-defined-for-the-elements-but-a-comparison-works/101423/4 "2023-07-10T10:55:03Z")

</div>

FIXED: `sort!` to `sort` thanks to mikmoore.

A good spec for `issorted` operation is to satisfy:

`sort(V) == V` → `issorted(V) == true`

if the stronger spec:

`sort(V) == V` ↔ `issorted(V) == true`

is adopted, then `issorted([NaN])` should be false.

But, as usual, `NaN` logic is tricky as hell, and perhaps erroring will do the developer a favor in pointing to a necessary fix before `NaN`s eat up the whole calculation.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [July 10, 2023, 3:02pm UTC](https://discourse.julialang.org/t/what-should-issorted-return-when-an-order-isnt-defined-for-the-elements-but-a-comparison-works/101423/5 "2023-07-10T15:02:09Z")

</div>

A container is (by default) considered sorted if `isless(x[i+1],x[i]) == false` for all valid `i`. Since `isless(NaN,NaN) == false`, it holds that `issorted([NaN,NaN]) == true`. More generally, any array of real floats is sorted if the non-`NaN` elements are sorted (including `-0.0` coming before `+0.0`) and any `NaN`s are at the very end. Note that any non-`NaN` is `isless` than `NaN` – e.g., `isless(Inf,NaN) == true`.

If you want check that an array is strictly increasing, try something like `all(i -> x[i] < x[i+1], eachindex(x)[begin:end-1])`.
