Skip to content

Commit

Permalink
Fix natural sorting of tuples (#568)
Browse files Browse the repository at this point in the history
- **fix natural sorting for tuples**
- **add tests**
- **bump patch version**
- **add changelog entry**
  • Loading branch information
jkrumbiegel authored Sep 25, 2024
1 parent 6ab480f commit 3db46be
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
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

2 comments on commit 3db46be

@jkrumbiegel
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/115943

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.8.11 -m "<description of version>" 3db46be04df656eff5c97475acfd53c155dc052a
git push origin v0.8.11

Please sign in to comment.