Skip to content

Commit

Permalink
Improve documentation of sort-related functions (#48387)
Browse files Browse the repository at this point in the history
* document the `order` keyword in `sort!`
* list explicitly the required properties of `lt` in `sort!`
* clarify the sequence of "by" transformations if both `by` and `order` are given
* show default values in the signatures for `searchsorted` and related functions
* note that `by` is also applied to searched value in `searchsorted` and related
* add `isunordered` to the manual (it's already exported)

---------

Co-authored-by: Lilith Orion Hafner <lilithhafner@gmail.com>
(cherry picked from commit a660798)
  • Loading branch information
knuesel authored and IanButterworth committed Aug 19, 2023
1 parent 747dea6 commit 10cbf3c
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 73 deletions.
12 changes: 6 additions & 6 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ isequal(x::AbstractFloat, y::Real ) = (isnan(x) & isnan(y)) | signequal(
isless(x, y)
Test whether `x` is less than `y`, according to a fixed total order (defined together with
[`isequal`](@ref)). `isless` is not defined on all pairs of values `(x, y)`. However, if it
[`isequal`](@ref)). `isless` is not defined for pairs `(x, y)` of all types. However, if it
is defined, it is expected to satisfy the following:
- If `isless(x, y)` is defined, then so is `isless(y, x)` and `isequal(x, y)`,
and exactly one of those three yields `true`.
Expand All @@ -154,13 +154,13 @@ Values that are normally unordered, such as `NaN`,
are ordered after regular values.
[`missing`](@ref) values are ordered last.
This is the default comparison used by [`sort`](@ref).
This is the default comparison used by [`sort!`](@ref).
# Implementation
Non-numeric types with a total order should implement this function.
Numeric types only need to implement it if they have special values such as `NaN`.
Types with a partial order should implement [`<`](@ref).
See the documentation on [Alternate orderings](@ref) for how to define alternate
See the documentation on [Alternate Orderings](@ref) for how to define alternate
ordering methods that can be used in sorting and related functions.
# Examples
Expand Down Expand Up @@ -335,6 +335,8 @@ New types with a canonical partial order should implement this function for
two arguments of the new type.
Types with a canonical total order should implement [`isless`](@ref) instead.
See also [`isunordered`](@ref).
# Examples
```jldoctest
julia> 'a' < 'b'
Expand Down Expand Up @@ -1352,7 +1354,7 @@ corresponding position in `collection`. To get a vector indicating whether each
in `items` is in `collection`, wrap `collection` in a tuple or a `Ref` like this:
`in.(items, Ref(collection))` or `items .∈ Ref(collection)`.
See also: [`∉`](@ref).
See also: [`∉`](@ref), [`insorted`](@ref), [`contains`](@ref), [`occursin`](@ref), [`issubset`](@ref).
# Examples
```jldoctest
Expand Down Expand Up @@ -1390,8 +1392,6 @@ julia> [1, 2] .∈ ([2, 3],)
0
1
```
See also: [`insorted`](@ref), [`contains`](@ref), [`occursin`](@ref), [`issubset`](@ref).
"""
in

Expand Down
8 changes: 4 additions & 4 deletions base/ordering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ By(by) = By(by, Forward)
"""
Lt(lt)
`Ordering` which calls `lt(a, b)` to compare elements. `lt` should
obey the same rules as implementations of [`isless`](@ref).
`Ordering` that calls `lt(a, b)` to compare elements. `lt` must
obey the same rules as the `lt` parameter of [`sort!`](@ref).
"""
struct Lt{T} <: Ordering
lt::T
Expand Down Expand Up @@ -146,8 +146,8 @@ Construct an [`Ordering`](@ref) object from the same arguments used by
Elements are first transformed by the function `by` (which may be
[`identity`](@ref)) and are then compared according to either the function `lt`
or an existing ordering `order`. `lt` should be [`isless`](@ref) or a function
which obeys similar rules. Finally, the resulting order is reversed if
`rev=true`.
that obeys the same rules as the `lt` parameter of [`sort!`](@ref). Finally,
the resulting order is reversed if `rev=true`.
Passing an `lt` other than `isless` along with an `order` other than
[`Base.Order.Forward`](@ref) or [`Base.Order.Reverse`](@ref) is not permitted,
Expand Down
Loading

0 comments on commit 10cbf3c

Please sign in to comment.