1
1
# Sorting and Related Functions
2
2
3
- Julia has an extensive, flexible API for sorting and interacting with already-sorted arrays of
4
- values. By default, Julia picks reasonable algorithms and sorts in standard ascending order:
3
+ Julia has an extensive, flexible API for sorting and interacting with already-sorted arrays
4
+ of values. By default, Julia picks reasonable algorithms and sorts in ascending order:
5
5
6
6
``` jldoctest
7
7
julia> sort([2,3,1])
@@ -11,7 +11,7 @@ julia> sort([2,3,1])
11
11
3
12
12
```
13
13
14
- You can easily sort in reverse order as well:
14
+ You can sort in reverse order as well:
15
15
16
16
``` jldoctest
17
17
julia> sort([2,3,1], rev=true)
@@ -36,8 +36,8 @@ julia> a
36
36
3
37
37
```
38
38
39
- Instead of directly sorting an array, you can compute a permutation of the array's indices that
40
- puts the array into sorted order:
39
+ Instead of directly sorting an array, you can compute a permutation of the array's
40
+ indices that puts the array into sorted order:
41
41
42
42
``` julia-repl
43
43
julia> v = randn(5)
@@ -65,7 +65,7 @@ julia> v[p]
65
65
0.382396
66
66
```
67
67
68
- Arrays can easily be sorted according to an arbitrary transformation of their values:
68
+ Arrays can be sorted according to an arbitrary transformation of their values:
69
69
70
70
``` julia-repl
71
71
julia> sort(v, by=abs)
@@ -101,9 +101,12 @@ julia> sort(v, alg=InsertionSort)
101
101
0.382396
102
102
```
103
103
104
- All the sorting and order related functions rely on a "less than" relation defining a total order
105
- on the values to be manipulated. The ` isless ` function is invoked by default, but the relation
106
- can be specified via the ` lt ` keyword.
104
+ All the sorting and order related functions rely on a "less than" relation defining a
105
+ [ strict weak order] ( https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings )
106
+ on the values to be manipulated. The ` isless ` function is invoked by default, but the
107
+ relation can be specified via the ` lt ` keyword, a function that takes two array elements
108
+ and returns ` true ` if and only if the first argument is "less than" the second. See
109
+ [ ` sort! ` ] ( @ref ) and [ Alternate Orderings] ( @ref ) for more information.
107
110
108
111
## Sorting Functions
109
112
@@ -165,22 +168,17 @@ Base.Sort.defalg(::AbstractArray{<:Union{SmallInlineStrings, Missing}}) = Inline
165
168
166
169
## Alternate Orderings
167
170
168
- By default, ` sort ` and related functions use [ ` isless ` ] ( @ref ) to compare two
169
- elements in order to determine which should come first. The
170
- [ ` Base.Order.Ordering ` ] ( @ref ) abstract type provides a mechanism for defining
171
- alternate orderings on the same set of elements: when calling a sorting function like
172
- ` sort ` , an instance of ` Ordering ` can be provided with the keyword argument ` order ` .
173
-
174
- Instances of ` Ordering ` define a [ total order] ( https://en.wikipedia.org/wiki/Total_order )
175
- on a set of elements, so that for any elements ` a ` , ` b ` , ` c ` the following hold:
176
-
177
- * Exactly one of the following is true: ` a ` is less than ` b ` , ` b ` is less than
178
- ` a ` , or ` a ` and ` b ` are equal (according to [ ` isequal ` ] ( @ref ) ).
179
- * The relation is transitive - if ` a ` is less than ` b ` and ` b ` is less than ` c `
180
- then ` a ` is less than ` c ` .
181
-
182
- The [ ` Base.Order.lt ` ] ( @ref ) function works as a generalization of ` isless ` to
183
- test whether ` a ` is less than ` b ` according to a given order.
171
+ By default, ` sort ` , ` searchsorted ` , and related functions use [ ` isless ` ] ( @ref ) to compare
172
+ two elements in order to determine which should come first. The
173
+ [ ` Base.Order.Ordering ` ] ( @ref ) abstract type provides a mechanism for defining alternate
174
+ orderings on the same set of elements: when calling a sorting function like
175
+ ` sort! ` , an instance of ` Ordering ` can be provided with the keyword argument ` order ` .
176
+
177
+ Instances of ` Ordering ` define an order through the [ ` Base.Order.lt ` ] ( @ref )
178
+ function, which works as a generalization of ` isless ` .
179
+ This function's behavior on custom ` Ordering ` s must satisfy all the conditions of a
180
+ [ strict weak order] ( https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings ) .
181
+ See [ ` sort! ` ] ( @ref ) for details and examples of valid and invalid ` lt ` functions.
184
182
185
183
``` @docs
186
184
Base.Order.Ordering
0 commit comments