Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix natural sorting of tuples #568

Merged
merged 6 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

## Unreleased

## v0.8.10 -2024-09-24
## v0.8.11 - 2024-09-25

- Fixed lexicographic natural sorting of tuples (this would fall back to default sort order before) [#568](https://github.com/MakieOrg/AlgebraOfGraphics.jl/pull/568).

## v0.8.10 - 2024-09-24

- Fixed markercolor in `ScatterLines` legends when it did not match `color` [#567](https://github.com/MakieOrg/AlgebraOfGraphics.jl/pull/567).

Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "AlgebraOfGraphics"
uuid = "cbdf2221-f076-402e-a563-3d30da359d67"
authors = ["Pietro Vertechi", "Julius Krumbiegel"]
version = "0.8.10"
version = "0.8.11"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand Down
15 changes: 15 additions & 0 deletions src/scales.jl
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,21 @@ push_different!(v, val) = !isempty(v) && isequal(last(v), val) || push!(v, val)
natural_lt(x, y) = isless(x, y)
natural_lt(x::AbstractString, y::AbstractString) = NaturalSort.natural(x, y)

# the below `natural_lt`s are copying Julia's `isless` implementation
natural_lt(::Tuple{}, ::Tuple{}) = false
natural_lt(::Tuple{}, ::Tuple) = true
natural_lt(::Tuple, ::Tuple{}) = false

# """
# natural_lt(t1::Tuple, t2::Tuple)

# Return `true` when `t1` is less than `t2` in lexicographic order.
# """
function natural_lt(t1::Tuple, t2::Tuple)
a, b = t1[1], t2[1]
natural_lt(a, b) || (isequal(a, b) && natural_lt(tail(t1), tail(t2)))
end

function mergesorted(v1, v2)
issorted(v1; lt = natural_lt) && issorted(v2; lt = natural_lt) || throw(ArgumentError("Arguments must be sorted"))
T = promote_type(eltype(v1), eltype(v2))
Expand Down
1 change: 1 addition & 0 deletions test/run_reference_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function compare_images(a::AbstractMatrix{<:Union{Makie.RGB,Makie.RGBA}}, b::Abs
end

function reftest(f::Function, name::String, update::Bool = get(ENV, "UPDATE_REFIMAGES", "false") == "true"; threshold = 0.05)
@info name
CairoMakie.activate!(px_per_unit = 1)
fig = with_theme(f, size = (400, 400))
path = joinpath(@__DIR__, "reference_tests")
Expand Down
6 changes: 6 additions & 0 deletions test/scales.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,10 @@ end
scale = scales[1]
@test scale isa CategoricalScale
@test scale.data == ["Id 1", "Id 2", "Id 10", "Id 21", "Id 100"]

@test sort(["1", "10", "2"]) == ["1", "10", "2"]
@test sort(["1", "10", "2"], lt = AlgebraOfGraphics.natural_lt) == ["1", "2", "10"]

@test sort([("1", 1), ("10", 2), ("2",3)]) == [("1", 1), ("10", 2), ("2", 3)]
@test sort([("1", 1), ("10", 2), ("2",3)], lt = AlgebraOfGraphics.natural_lt) == [("1", 1), ("2", 3), ("10", 2)]
end
Loading