64
64
issorted(v, lt=isless, by=identity, rev::Bool=false, order::Ordering=Forward)
65
65
66
66
Test whether a vector is in sorted order. The `lt`, `by` and `rev` keywords modify what
67
- order is considered to be sorted just as they do for [`sort`](@ref).
67
+ order is considered to be sorted just as they do for [`sort! `](@ref).
68
68
69
69
# Examples
70
70
```jldoctest
@@ -94,14 +94,17 @@ maybeview(v, k) = view(v, k)
94
94
maybeview (v, k:: Integer ) = v[k]
95
95
96
96
"""
97
- partialsort!(v, k; by=<transform> , lt=<comparison> , rev=false)
97
+ partialsort!(v, k; by=identity , lt=isless , rev=false)
98
98
99
- Partially sort the vector `v` in place, according to the order specified by `by`, `lt` and
100
- `rev` so that the value at index `k` (or range of adjacent values if `k` is a range) occurs
99
+ Partially sort the vector `v` in place so that the value at index `k` (or
100
+ range of adjacent values if `k` is a range) occurs
101
101
at the position where it would appear if the array were fully sorted. If `k` is a single
102
102
index, that value is returned; if `k` is a range, an array of values at those indices is
103
103
returned. Note that `partialsort!` may not fully sort the input array.
104
104
105
+ For the keyword arguments, see the documentation of [`sort!`](@ref).
106
+
107
+
105
108
# Examples
106
109
```jldoctest
107
110
julia> a = [1, 2, 4, 3, 4]
@@ -148,9 +151,9 @@ partialsort!(v::AbstractVector, k::Union{Integer,OrdinalRange};
148
151
partialsort! (v, k, ord (lt,by,rev,order))
149
152
150
153
"""
151
- partialsort(v, k, by=<transform> , lt=<comparison> , rev=false)
154
+ partialsort(v, k, by=identity , lt=isless , rev=false)
152
155
153
- Variant of [`partialsort!`](@ref) which copies `v` before partially sorting it, thereby returning the
156
+ Variant of [`partialsort!`](@ref) that copies `v` before partially sorting it, thereby returning the
154
157
same thing as `partialsort!` but leaving `v` unmodified.
155
158
"""
156
159
partialsort (v:: AbstractVector , k:: Union{Integer,OrdinalRange} ; kws... ) =
@@ -288,14 +291,16 @@ for s in [:searchsortedfirst, :searchsortedlast, :searchsorted]
288
291
end
289
292
290
293
"""
291
- searchsorted(a, x; by=<transform> , lt=<comparison> , rev=false)
294
+ searchsorted(a, x; by=identity , lt=isless , rev=false)
292
295
293
- Return the range of indices of `a` which compare as equal to `x` (using binary search)
294
- according to the order specified by the `by`, `lt` and `rev` keywords, assuming that `a`
295
- is already sorted in that order. Return an empty range located at the insertion point
296
- if `a` does not contain values equal to `x`.
296
+ Return the range of indices of `a` that compare as equal to `x` (using binary
297
+ search), assuming that `a` is already sorted. Return an empty range located at
298
+ the insertion point if `a` does not contain values equal to `x`.
297
299
298
- See also: [`insorted`](@ref), [`searchsortedfirst`](@ref), [`sort`](@ref), [`findall`](@ref).
300
+ The `by`, `lt` and `rev` keywords modify what order is assumed for the data,
301
+ as described in the [`sort!`](@ref) documentation.
302
+
303
+ See also: [`insorted`](@ref), [`searchsortedfirst`](@ref), [`sort!`](@ref), [`findall`](@ref).
299
304
300
305
# Examples
301
306
```jldoctest
@@ -317,14 +322,17 @@ julia> searchsorted([1, 2, 4, 5, 5, 7], 0) # no match, insert at start
317
322
""" searchsorted
318
323
319
324
"""
320
- searchsortedfirst(a, x; by=<transform> , lt=<comparison> , rev=false)
325
+ searchsortedfirst(a, x; by=identity , lt=isless , rev=false)
321
326
322
- Return the index of the first value in `a` greater than or equal to `x`, according to the
323
- specified order . Return `lastindex(a) + 1` if `x` is greater than all values in `a`.
324
- `a` is assumed to be sorted .
327
+ Return the index of the first value in `a` greater than or equal to `x`,
328
+ assuming that `a` is already sorted . Return `lastindex(a) + 1` if `x` is
329
+ greater than all values in `a` .
325
330
326
331
`insert!`ing `x` at this index will maintain sorted order.
327
332
333
+ The `by`, `lt` and `rev` keywords modify what order is assumed for the data,
334
+ as described in the [`sort!`](@ref) documentation.
335
+
328
336
See also: [`searchsortedlast`](@ref), [`searchsorted`](@ref), [`findfirst`](@ref).
329
337
330
338
# Examples
@@ -347,11 +355,12 @@ julia> searchsortedfirst([1, 2, 4, 5, 5, 7], 0) # no match, insert at start
347
355
""" searchsortedfirst
348
356
349
357
"""
350
- searchsortedlast(a, x; by=<transform> , lt=<comparison> , rev=false)
358
+ searchsortedlast(a, x; by=identity , lt=isless , rev=false)
351
359
352
- Return the index of the last value in `a` less than or equal to `x`, according to the
353
- specified order. Return `firstindex(a) - 1` if `x` is less than all values in `a`. `a` is
354
- assumed to be sorted.
360
+ Return the index of the last value in `a` less than or equal to `x`, assuming
361
+ that `a` is already sorted. Return `firstindex(a) - 1` if `x` is less than all
362
+ values in `a`. The `by`, `lt` and `rev` keywords modify what order is assumed
363
+ for the data, as described in the [`sort!`](@ref) documentation.
355
364
356
365
# Examples
357
366
```jldoctest
@@ -373,12 +382,12 @@ julia> searchsortedlast([1, 2, 4, 5, 5, 7], 0) # no match, insert at start
373
382
""" searchsortedlast
374
383
375
384
"""
376
- insorted(x, a; by=<transform> , lt=<comparison> , rev=false) -> Bool
385
+ insorted(x, a; by=identity , lt=isless , rev=false) -> Bool
377
386
378
387
Determine whether an item `x` is in the sorted collection `a`, in the sense that
379
- it is [`==`](@ref) to one of the values of the collection according to the order
380
- specified by the `by`, `lt` and `rev` keywords, assuming that `a` is already
381
- sorted in that order, see [`sort`](@ref) for the keywords .
388
+ it is [`==`](@ref) to one of the values of the collection. The `by`, `lt` and
389
+ `rev` keywords modify what order is assumed for the collection, as described in
390
+ the [`sort! `](@ref) documentation .
382
391
383
392
See also [`in`](@ref).
384
393
@@ -524,7 +533,7 @@ Base.size(v::WithoutMissingVector) = size(v.data)
524
533
send_to_end!(f::Function, v::AbstractVector; [lo, hi])
525
534
526
535
Send every element of `v` for which `f` returns `true` to the end of the vector and return
527
- the index of the last element which for which `f` returns `false`.
536
+ the index of the last element for which `f` returns `false`.
528
537
529
538
`send_to_end!(f, v, lo, hi)` is equivalent to `send_to_end!(f, view(v, lo:hi))+lo-1`
530
539
@@ -724,8 +733,8 @@ Insertion sort traverses the collection one element at a time, inserting
724
733
each element into its correct, sorted position in the output vector.
725
734
726
735
Characteristics:
727
- * *stable*: preserves the ordering of elements which compare equal
728
- (e.g. "a" and "A" in a sort of letters which ignores case).
736
+ * *stable*: preserves the ordering of elements that compare equal
737
+ (e.g. "a" and "A" in a sort of letters that ignores case).
729
738
* *in-place* in memory.
730
739
* *quadratic performance* in the number of elements to be sorted:
731
740
it is well-suited to small collections but should not be used for large ones.
@@ -965,8 +974,8 @@ is treated as the first or last index of the input, respectively.
965
974
`lo` and `hi` may be specified together as an `AbstractUnitRange`.
966
975
967
976
Characteristics:
968
- * *stable*: preserves the ordering of elements which compare equal
969
- (e.g. "a" and "A" in a sort of letters which ignores case).
977
+ * *stable*: preserves the ordering of elements that compare equal
978
+ (e.g. "a" and "A" in a sort of letters that ignores case).
970
979
* *not in-place* in memory.
971
980
* *divide-and-conquer*: sort strategy similar to [`QuickSort`](@ref).
972
981
* *linear runtime* if `length(lo:hi)` is constant
@@ -1242,7 +1251,7 @@ Otherwise, we dispatch to [`InsertionSort`](@ref) for inputs with `length <= 40`
1242
1251
perform a presorted check ([`CheckSorted`](@ref)).
1243
1252
1244
1253
We check for short inputs before performing the presorted check to avoid the overhead of the
1245
- check for small inputs. Because the alternate dispatch is to [`InseritonSort `](@ref) which
1254
+ check for small inputs. Because the alternate dispatch is to [`InsertionSort `](@ref) which
1246
1255
has efficient `O(n)` runtime on presorted inputs, the check is not necessary for small
1247
1256
inputs.
1248
1257
@@ -1323,15 +1332,31 @@ defalg(v::AbstractArray{Union{}}) = DEFAULT_UNSTABLE # for method disambiguation
1323
1332
"""
1324
1333
sort!(v; alg::Algorithm=defalg(v), lt=isless, by=identity, rev::Bool=false, order::Ordering=Forward)
1325
1334
1326
- Sort the vector `v` in place. A stable algorithm is used by default. You can select a
1327
- specific algorithm to use via the `alg` keyword (see [Sorting Algorithms](@ref) for
1328
- available algorithms). The `by` keyword lets you provide a function that will be applied to
1329
- each element before comparison; the `lt` keyword allows providing a custom "less than"
1330
- function (note that for every `x` and `y`, only one of `lt(x,y)` and `lt(y,x)` can return
1331
- `true`); use `rev=true` to reverse the sorting order. These options are independent and can
1332
- be used together in all possible combinations: if both `by` and `lt` are specified, the `lt`
1333
- function is applied to the result of the `by` function; `rev=true` reverses whatever
1334
- ordering specified via the `by` and `lt` keywords.
1335
+ Sort the vector `v` in place. A stable algorithm is used by default. A specific
1336
+ algorithm can be selected via the `alg` keyword (see [Sorting Algorithms](@ref)
1337
+ for available algorithms). Elements are first transformed by the function `by`
1338
+ and then compared according to either the function `lt` or the ordering
1339
+ `order`. Finally, the resulting order is reversed if `rev=true`.
1340
+
1341
+ The `lt` function should define a strict partial order, that is, it should be
1342
+
1343
+ - irreflexive: `lt(x, x)` always yields `false`,
1344
+ - asymmetric: if `lt(x, y)` yields `true` then `lt(y, x)` yields `false`,
1345
+ - transitive: `lt(x, y) && lt(y, z)` implies `lt(x, z)`.
1346
+
1347
+ For example `<` is a valid `lt` function but `≤` is not.
1348
+
1349
+ Passing an `lt` other than `isless` along with an `order` other than
1350
+ [`Base.Order.Forward`](@ref) or [`Base.Order.Reverse`](@ref) is not permitted,
1351
+ otherwise all options are independent and can be used together in all possible
1352
+ combinations. Note that `order` can also include a "by" transformation, in
1353
+ which case it is applied after that defined with the `by` keyword. For more
1354
+ information on `order` values see the documentation on [Alternate
1355
+ Orderings](@ref).
1356
+
1357
+ See also [`sort`](@ref), [`sortperm`](@ref), [`sortslices`](@ref),
1358
+ [`partialsort!`](@ref), [`partialsortperm`](@ref), [`issorted`](@ref),
1359
+ [`searchsorted`](@ref), [`insorted`](@ref), [`Base.Order.ord`](@ref).
1335
1360
1336
1361
# Examples
1337
1362
```jldoctest
@@ -1358,6 +1383,13 @@ julia> v = [(1, "c"), (3, "a"), (2, "b")]; sort!(v, by = x -> x[2]); v
1358
1383
(3, "a")
1359
1384
(2, "b")
1360
1385
(1, "c")
1386
+
1387
+ julia> sort(0:3, by=x->x-2, order=Base.Order.By(abs)) # same as sort(0:3, by=abs(x->x-2))
1388
+ 4-element Vector{Int64}:
1389
+ 2
1390
+ 1
1391
+ 3
1392
+ 0
1361
1393
```
1362
1394
"""
1363
1395
function sort! (v:: AbstractVector{T} ;
@@ -1398,15 +1430,15 @@ sort(v::AbstractVector; kws...) = sort!(copymutable(v); kws...)
1398
1430
# # partialsortperm: the permutation to sort the first k elements of an array ##
1399
1431
1400
1432
"""
1401
- partialsortperm(v, k; by=<transform> , lt=<comparison> , rev=false)
1433
+ partialsortperm(v, k; by=ientity , lt=isless , rev=false)
1402
1434
1403
1435
Return a partial permutation `I` of the vector `v`, so that `v[I]` returns values of a fully
1404
1436
sorted version of `v` at index `k`. If `k` is a range, a vector of indices is returned; if
1405
1437
`k` is an integer, a single index is returned. The order is specified using the same
1406
1438
keywords as `sort!`. The permutation is stable, meaning that indices of equal elements
1407
1439
appear in ascending order.
1408
1440
1409
- Note that this function is equivalent to, but more efficient than, calling `sortperm(...)[k]`.
1441
+ This function is equivalent to, but more efficient than, calling `sortperm(...)[k]`.
1410
1442
1411
1443
# Examples
1412
1444
```jldoctest
@@ -1432,7 +1464,7 @@ partialsortperm(v::AbstractVector, k::Union{Integer,OrdinalRange}; kwargs...) =
1432
1464
partialsortperm! (similar (Vector{eltype (k)}, axes (v,1 )), v, k; kwargs... )
1433
1465
1434
1466
"""
1435
- partialsortperm!(ix, v, k; by=<transform> , lt=<comparison> , rev=false)
1467
+ partialsortperm!(ix, v, k; by=identity , lt=isless , rev=false)
1436
1468
1437
1469
Like [`partialsortperm`](@ref), but accepts a preallocated index vector `ix` the same size as
1438
1470
`v`, which is used to store (a permutation of) the indices of `v`.
@@ -1732,7 +1764,8 @@ end
1732
1764
sort!(A; dims::Integer, alg::Algorithm=defalg(A), lt=isless, by=identity, rev::Bool=false, order::Ordering=Forward)
1733
1765
1734
1766
Sort the multidimensional array `A` along dimension `dims`.
1735
- See [`sort!`](@ref) for a description of possible keyword arguments.
1767
+ See the vector version of [`sort!`](@ref) for a description of possible keyword
1768
+ arguments.
1736
1769
1737
1770
To sort slices of an array, refer to [`sortslices`](@ref).
1738
1771
@@ -1886,8 +1919,8 @@ algorithm. Partial quick sort returns the smallest `k` elements sorted from smal
1886
1919
to largest, finding them and sorting them using [`QuickSort`](@ref).
1887
1920
1888
1921
Characteristics:
1889
- * *not stable*: does not preserve the ordering of elements which
1890
- compare equal (e.g. "a" and "A" in a sort of letters which
1922
+ * *not stable*: does not preserve the ordering of elements that
1923
+ compare equal (e.g. "a" and "A" in a sort of letters that
1891
1924
ignores case).
1892
1925
* *in-place* in memory.
1893
1926
* *divide-and-conquer*: sort strategy similar to [`MergeSort`](@ref).
@@ -1903,8 +1936,8 @@ Indicate that a sorting function should use the quick sort
1903
1936
algorithm, which is *not* stable.
1904
1937
1905
1938
Characteristics:
1906
- * *not stable*: does not preserve the ordering of elements which
1907
- compare equal (e.g. "a" and "A" in a sort of letters which
1939
+ * *not stable*: does not preserve the ordering of elements that
1940
+ compare equal (e.g. "a" and "A" in a sort of letters that
1908
1941
ignores case).
1909
1942
* *in-place* in memory.
1910
1943
* *divide-and-conquer*: sort strategy similar to [`MergeSort`](@ref).
@@ -1922,8 +1955,8 @@ subcollection at each step, until the entire
1922
1955
collection has been recombined in sorted form.
1923
1956
1924
1957
Characteristics:
1925
- * *stable*: preserves the ordering of elements which compare
1926
- equal (e.g. "a" and "A" in a sort of letters which ignores
1958
+ * *stable*: preserves the ordering of elements that compare
1959
+ equal (e.g. "a" and "A" in a sort of letters that ignores
1927
1960
case).
1928
1961
* *not in-place* in memory.
1929
1962
* *divide-and-conquer* sort strategy.
0 commit comments