From b38fde1ad42c977878d4f481c962b108a3ae20ab Mon Sep 17 00:00:00 2001 From: Neven Sajko Date: Fri, 25 Oct 2024 09:26:36 +0200 Subject: [PATCH] support `isless` for zero-dimensional `AbstractArray`s (#55772) Fixes #55771 --- NEWS.md | 1 + base/abstractarray.jl | 9 +++++++++ test/arrayops.jl | 8 ++++++++ 3 files changed, 18 insertions(+) diff --git a/NEWS.md b/NEWS.md index cf04fbf577248..658dcc7aa320e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -117,6 +117,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 e877a87c2cdd1..cbbae8e852b2e 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -3044,6 +3044,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 ec8f54828b965..49d51176dcf71 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -1409,6 +1409,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