# Trouble with isless for an immutable type

**URL:** <https://discourse.julialang.org/t/trouble-with-isless-for-an-immutable-type/994>\
**Category:** General Usage\
**Created:** [December 16, 2016, 3:19pm UTC](https://discourse.julialang.org/t/trouble-with-isless-for-an-immutable-type/994 "2016-12-16T15:19:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigschmidt](https://avatars.discourse-cdn.com/v4/letter/c/3da27b/32.png) [@craigschmidt](https://discourse.julialang.org/u/craigschmidt)\
**Post date:** [December 16, 2016, 3:19pm UTC](https://discourse.julialang.org/t/trouble-with-isless-for-an-immutable-type/994/1 "2016-12-16T15:19:30Z")

</div>

I feel like I’m missing something obvious, but why doesn’t this work:

```julia
julia> immutable Counts
          j::Int32
          i::Int32
          nij::Int64
       end

julia> # allocate something
       test = Array{Counts,1}()
0-element Array{Counts,1}

julia> for i in 1:10
               push!(test,Counts(rand(Int32),rand(Int32),rand(Int64)))
       end

julia> # is x < y ?
       isless(x::Counts, y::Counts) = (x.j < y.j) || ((x.j == y.j) && (x.i < y.i)) || ((x.j == y.j) && (x.i == y.i) && (x.nij < y.nij))
isless (generic function with 1 method)

julia> sort!(test)
ERROR: MethodError: no method matching isless(::Counts, ::Counts)
 in sort!(::Array{Counts,1}, ::Int64, ::Int64, ::Base.Sort.InsertionSortAlg, ::Base.Order.ForwardOrdering) at ./sort.jl:222
 in sort!(::Array{Counts,1}, ::Int64, ::Int64, ::Base.Sort.MergeSortAlg, ::Base.Order.ForwardOrdering, ::Array{Counts,1}) at ./sort.jl:311
 in sort!(::Array{Counts,1}, ::Base.Sort.MergeSortAlg, ::Base.Order.ForwardOrdering) at ./sort.jl:405
 in sort!(::Array{Counts,1}) at ./sort.jl:414

```

I define a simple immutable type, and just want to sort an array of them  
in lexicographic order.

Am I defining isless wrong somehow?

Thanks,  
Craig

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [December 16, 2016, 3:23pm UTC](https://discourse.julialang.org/t/trouble-with-isless-for-an-immutable-type/994/2 "2016-12-16T15:23:07Z")

</div>

Try this:  
`Base.isless(x::Counts, y::Counts) = (x.j < y.j) || ...`

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 16, 2016, 3:28pm UTC](https://discourse.julialang.org/t/trouble-with-isless-for-an-immutable-type/994/3 "2016-12-16T15:28:43Z")

</div>

Or

```julia
import Base.isless

isless(x::Counts, y::Counts) = (x.j < y.j) || ((x.j == y.j) && (x.i < y.i)) || ((x.j == y.j) && (x.i == y.i) && (x.nij < y.nij))

```

---

<div class="post-metadata">

**Author:** ![craigschmidt](https://avatars.discourse-cdn.com/v4/letter/c/3da27b/32.png) [@craigschmidt](https://discourse.julialang.org/u/craigschmidt)\
**Post date:** [December 16, 2016, 3:44pm UTC](https://discourse.julialang.org/t/trouble-with-isless-for-an-immutable-type/994/4 "2016-12-16T15:44:32Z")

</div>

Thanks for the quick reply. I thought there was an implicit ‘using Base’, but apparently not. I’m still new to Julia.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 16, 2016, 3:49pm UTC](https://discourse.julialang.org/t/trouble-with-isless-for-an-immutable-type/994/5 "2016-12-16T15:49:46Z")

</div>

There is an implicit `using Base` but it doesn’t import all the symbols for extending so that adding a function in `Base` won’t be a breaking change. Ref [http://docs.julialang.org/en/stable/manual/modules/](http://docs.julialang.org/en/stable/manual/modules/)
