diff --git a/NEWS.md b/NEWS.md index c12cc3c64300c..0e63ec4d0afa8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -103,6 +103,7 @@ New library features * `RegexMatch` objects can now be used to construct `NamedTuple`s and `Dict`s ([#50988]) * `Lockable` is now exported ([#54595]) * New `ltruncate`, `rtruncate` and `ctruncate` functions for truncating strings to text width, accounting for char widths ([#55351]) +* `isless` (and thus `cmp`, sorting, etc.) is now supported for zero-dimensional `AbstractArray`s ([#55772]) Standard library changes ------------------------ diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 754ab20660ab8..fd08ecc844201 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -3047,6 +3047,15 @@ function cmp(A::AbstractVector, B::AbstractVector) return cmp(length(A), length(B)) end +""" + isless(A::AbstractArray{<:Any,0}, B::AbstractArray{<:Any,0}) + +Return `true` when the only element of `A` is less than the only element of `B`. +""" +function isless(A::AbstractArray{<:Any,0}, B::AbstractArray{<:Any,0}) + isless(only(A), only(B)) +end + """ isless(A::AbstractVector, B::AbstractVector) diff --git a/test/arrayops.jl b/test/arrayops.jl index 333b68e287c4c..0e365d8690f9c 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -1404,6 +1404,14 @@ end end @testset "lexicographic comparison" begin + @testset "zero-dimensional" begin + vals = (0, 0.0, 1, 1.0) + for l ∈ vals + for r ∈ vals + @test cmp(fill(l), fill(r)) == cmp(l, r) + end + end + end @test cmp([1.0], [1]) == 0 @test cmp([1], [1.0]) == 0 @test cmp([1, 1], [1, 1]) == 0