Sorts an array along a dimension.
You can also use sort() directly.
Usage
nv_sort(operand, dim = NULL, decreasing = FALSE, stable = FALSE)
# S3 method for class 'AnvlArray'
sort(x, decreasing = FALSE, ..., dim = NULL)Arguments
- operand
(
arrayish)
Operand.- dim
(
integer(1)|NULL)
Dimension along which to sort. IfNULL(default), uses the last dimension.- decreasing
(
logical(1))
IfTRUE, sort in decreasing order.- stable
(
logical(1))
IfTRUE, the sort is stable: equal values keep their original relative order alongdim. DefaultFALSE. Stability is only observable for floats when-0/+0or-NaN/+NaNare mixed (they compare equal under the total order used here); for distinct values the result is identical either way.- x
(
arrayish)
Same asoperand; this is the name used by the base R S3 generic.- ...
No additional arguments.
Value
arrayish
Same shape and data type as operand.
NaN handling
NaN values sort to the end (ascending) or beginning
(descending), regardless of sign. +0 and -0 compare equal.
See also
prim_sort() for the underlying primitive,
nv_argsort(), nv_top_k(), nv_median(),
nv_argmax(), nv_argmin().
Examples
x <- nv_array(c(3, 1, 4, 1, 5, 9, 2, 6))
nv_sort(x)
#> AnvlArray
#> 1
#> 1
#> 2
#> 3
#> 4
#> 5
#> 6
#> 9
#> [ CPUf32{8} ]
sort(x) # via the S3 generic
#> AnvlArray
#> 1
#> 1
#> 2
#> 3
#> 4
#> 5
#> 6
#> 9
#> [ CPUf32{8} ]
nv_sort(x, decreasing = TRUE)
#> AnvlArray
#> 9
#> 6
#> 5
#> 4
#> 3
#> 2
#> 1
#> 1
#> [ CPUf32{8} ]
m <- nv_matrix(c(3, 1, 5, 2, 4, 0), nrow = 2, byrow = TRUE)
nv_sort(m, dim = 2L)
#> AnvlArray
#> 1 3 5
#> 0 2 4
#> [ CPUf32{2,3} ]