# Is there a method to check if a vector is "sortable"?

**URL:** <https://discourse.julialang.org/t/is-there-a-method-to-check-if-a-vector-is-sortable/56127>\
**Category:** General Usage\
**Tags:** question, sort\
**Created:** [February 27, 2021, 7:53am UTC](https://discourse.julialang.org/t/is-there-a-method-to-check-if-a-vector-is-sortable/56127 "2021-02-27T07:53:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [February 27, 2021, 7:53am UTC](https://discourse.julialang.org/t/is-there-a-method-to-check-if-a-vector-is-sortable/56127/1 "2021-02-27T07:53:46Z")

</div>

In a Decision Tree algorithm I implement an optimisation where I first sort the vector of data once and then I pick up the various items with `searchsorted`.  
All fine for integers, floats, even strings… but If the vector is of custom types for which no `isless` is defined it shouldn’t work.

Is there then a method I can first check on a `Vector{T}` to see if the vector is sortable, without relying to use exceptions ?

The Julia documentation concerning “sorting” is [here](https://docs.julialang.org/en/v1/base/sort/), but I couldn’t find any reference to a `issortable` function 🙂

---

<div class="post-metadata">

**Author:** ![healyp](https://avatars.discourse-cdn.com/v4/letter/h/67e7ee/32.png) [@healyp](https://discourse.julialang.org/u/healyp)\
**Post date:** [February 27, 2021, 8:02am UTC](https://discourse.julialang.org/t/is-there-a-method-to-check-if-a-vector-is-sortable/56127/2 "2021-02-27T08:02:55Z")

</div>

If the custom type is defined by you can you write your own `isless(a::T, b::T) where {T}` function?

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [February 27, 2021, 8:13am UTC](https://discourse.julialang.org/t/is-there-a-method-to-check-if-a-vector-is-sortable/56127/3 "2021-02-27T08:13:44Z")

</div>

Unfortunately this is for a library and I don’t know the type in advance…

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [February 27, 2021, 8:22am UTC](https://discourse.julialang.org/t/is-there-a-method-to-check-if-a-vector-is-sortable/56127/4 "2021-02-27T08:22:32Z")

</div>

But you can check if `isless` is defined for the elements in the vector, for example with `applicable`.

---

<div class="post-metadata">

**Author:** ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)\
**Post date:** [February 27, 2021, 8:25am UTC](https://discourse.julialang.org/t/is-there-a-method-to-check-if-a-vector-is-sortable/56127/5 "2021-02-27T08:25:00Z")

</div>

You could check whether there is a `isless` method for the element type, i.e., `hasmethod(isless, Tuple{T,T})`.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [February 27, 2021, 9:11am UTC](https://discourse.julialang.org/t/is-there-a-method-to-check-if-a-vector-is-sortable/56127/6 "2021-02-27T09:11:43Z")

</div>

> [@sostock](#):
>
> hasmethod(isless, Tuple{T,T})

Thanks. This works:

```julia
mutable struct SortableType
    x::Int64
    y::Int64
end

mutable struct UnsortableType
    x::Int64
    y::Int64
end
isless(x::SortableType,y::SortableType) = x.x < y.x

SortableVector = [SortableType(2,4),SortableType(1,5),SortableType(1,8),missing]
UnsortableVector = [UnsortableType(2,4),UnsortableType(1,5),missing,UnsortableType(1,8)]

issortable(::AbstractArray{T,N}) where {T,N} = hasmethod(isless, Tuple{nonmissingtype(T),nonmissingtype(T)})
issortable(SortableVector) # true
issortable(UnsortableVector) # false

```

[Edited: added support for `Unions{T,Missing}`]  
[Edited 2: using `AbstractArray` instead of `Array`]

---

<div class="post-metadata">

**Author:** ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)\
**Post date:** [February 27, 2021, 12:23pm UTC](https://discourse.julialang.org/t/is-there-a-method-to-check-if-a-vector-is-sortable/56127/7 "2021-02-27T12:23:35Z")

</div>

If this has to be fast, you could use `static_hasmethod` from [Tricks.jl](https://github.com/oxinabox/Tricks.jl).
