Skip to contents

Sorts an array along an axis.

You can also use sort() directly.

Usage

nv_sort(x, axis = NULL, decreasing = FALSE, stable = FALSE)

# S3 method for class 'AnvlArray'
sort(x, decreasing = FALSE, ..., axis = NULL)

Arguments

x

(arrayish)
Input array.

axis

(integer(1) | NULL)
Axis along which to sort. Negative values count from the end, i.e. -1 refers to the last axis. If NULL (default), uses the last axis.

decreasing

(logical(1))
If TRUE, sort in decreasing order.

stable

(logical(1))
If TRUE, the sort is stable: equal values keep their original relative order along axis. Default FALSE. Stability is only observable for floats when -0 / +0 or -NaN / +NaN are mixed (they compare equal under the total order used here); for distinct values the result is identical either way.

...

No additional arguments.

Value

arrayish
Same shape and data type as x.

NaN handling

NaN values sort to the end (ascending) or beginning (descending), regardless of sign. +0 and -0 compare equal.

See also

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, axis = 2L)
#> AnvlArray
#>  1 3 5
#>  0 2 4
#> [ CPUf32{2,3} ]