diff --git a/base/abstractarray.jl b/base/abstractarray.jl index add8ecb47a341..b5db05bd09ea2 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -3020,6 +3020,8 @@ function isequal(A::AbstractArray, B::AbstractArray) end function cmp(A::AbstractVector, B::AbstractVector) + ai1, bi1 = firstindex(A), firstindex(B) + isequal(ai1, bi1) || return cmp(ai1, bi1) for (a, b) in zip(A, B) if !isequal(a, b) return isless(a, b) ? -1 : 1 @@ -3040,7 +3042,8 @@ end """ isless(A::AbstractVector, B::AbstractVector) -Return `true` when `A` is less than `B` in lexicographic order. +Return `true` when `A` is less than `B`. Vectors are first compared by +their starting indices, and then lexicographically by their elements. """ isless(A::AbstractVector, B::AbstractVector) = cmp(A, B) < 0 diff --git a/test/arrayops.jl b/test/arrayops.jl index e6f8e9ec6756d..d4b401152d7ef 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -1465,6 +1465,18 @@ end @test cmp([UInt8(1), UInt8(0)], [UInt8(0), UInt8(0)]) == 1 @test cmp([UInt8(1), UInt8(0)], [UInt8(1), UInt8(0)]) == 0 @test cmp([UInt8(0), UInt8(0)], [UInt8(1), UInt8(1)]) == -1 + + x = [1, 2, 3] + y = OffsetVector(x, -1) + @test cmp(x, y) == 1 + @test cmp(y, x) == -1 + @test !isless(x, y) + @test isless(y, x) + + y2 = OffsetVector([1, 2, 3], 0) + @test cmp(x, y2) == 0 + @test !isless(x, y2) + @test !isless(y2, x) end @testset "sort on arrays" begin