Julia: define isless and other comparison operators for Arrays

Created on 26 Apr 2016  路  5Comments  路  Source: JuliaLang/julia

Define a comparison operators <,> etc for Arrays with obvious semantics:

 aa = [1., 2.]
 bb = [3., 1.]
 aa < bb

ERROR: MethodError: isless has no method matching isless(::Array{Float64,1}, ::Array{Float64,1})
Closest candidates are:
isless(::DataArrays.NAtype, ::Any)
isless(::Any, ::DataArrays.NAtype)
in < at operators.jl:33

All 5 comments

Use the existing .< operator.

There's also lexless, which I think has the semantics that you want.

Yes, arrays do not have a canonical order, so the user should be explicit about what they mean.

@johnmyleswhite thanks a lot :)

For anyone else reading this: In Julia 1.0, cmp (and hence isless) is defined for vectors:

julia> isless([1,2], [2,1])
true
File: base/abstractarray.jl
1674: function cmp(A::AbstractVector, B::AbstractVector)
1675:     for (a, b) in zip(A, B)
1676:         if !isequal(a, b)
1677:             return isless(a, b) ? -1 : 1
1678:         end
1679:     end
1680:     return cmp(length(A), length(B))
1681: end
Was this page helpful?
0 / 5 - 0 ratings