11# Sorting and Related Functions
22
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:
55
66``` jldoctest
77julia> sort([2,3,1])
@@ -11,7 +11,7 @@ julia> sort([2,3,1])
1111 3
1212```
1313
14- You can easily sort in reverse order as well:
14+ You can sort in reverse order as well:
1515
1616``` jldoctest
1717julia> sort([2,3,1], rev=true)
@@ -36,8 +36,8 @@ julia> a
3636 3
3737```
3838
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:
4141
4242``` julia-repl
4343julia> v = randn(5)
@@ -65,7 +65,7 @@ julia> v[p]
6565 0.382396
6666```
6767
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:
6969
7070``` julia-repl
7171julia> sort(v, by=abs)
@@ -101,9 +101,12 @@ julia> sort(v, alg=InsertionSort)
101101 0.382396
102102```
103103
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.
107110
108111## Sorting Functions
109112
@@ -165,22 +168,17 @@ Base.Sort.defalg(::AbstractArray{<:Union{SmallInlineStrings, Missing}}) = Inline
165168
166169## Alternate Orderings
167170
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.
184182
185183``` @docs
186184Base.Order.Ordering
0 commit comments