|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2026 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# toSorted |
| 22 | + |
| 23 | +> Return a new [ndarray][@stdlib/ndarray/ctor] containing the elements of an input [ndarray][@stdlib/ndarray/ctor] sorted along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var toSorted = require( '@stdlib/blas/ext/to-sorted' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### toSorted( x\[, sortOrder]\[, options] ) |
| 34 | + |
| 35 | +Returns a new [ndarray][@stdlib/ndarray/ctor] containing the elements of an input [ndarray][@stdlib/ndarray/ctor] sorted along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var array = require( '@stdlib/ndarray/array' ); |
| 39 | + |
| 40 | +var x = array( [ -1.0, 2.0, -3.0 ] ); |
| 41 | + |
| 42 | +var y = toSorted( x ); |
| 43 | +// returns <ndarray>[ -3.0, -1.0, 2.0 ] |
| 44 | +``` |
| 45 | + |
| 46 | +The function has the following parameters: |
| 47 | + |
| 48 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. |
| 49 | +- **sortOrder**: sort order (_optional_). May be either a scalar value, string, or an [ndarray][@stdlib/ndarray/ctor] having a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] sort order must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] sort order must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. By default, the sort order is `1` (i.e., increasing order). |
| 50 | +- **options**: function options (_optional_). |
| 51 | + |
| 52 | +The function accepts the following options: |
| 53 | + |
| 54 | +- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. |
| 55 | +- **dtype**: output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes]. |
| 56 | + |
| 57 | +By default, the function sorts elements in increasing order. To sort in a different order, provide a `sortOrder` argument. |
| 58 | + |
| 59 | +```javascript |
| 60 | +var array = require( '@stdlib/ndarray/array' ); |
| 61 | + |
| 62 | +var x = array( [ -1.0, 2.0, -3.0 ] ); |
| 63 | + |
| 64 | +var y = toSorted( x, -1.0 ); |
| 65 | +// returns <ndarray>[ 2.0, -1.0, -3.0 ] |
| 66 | +``` |
| 67 | + |
| 68 | +In addition to numeric values, one can specify the sort order via one of the following string literals: `'ascending'`, `'asc'`, `'descending'`, or `'desc'`. The first two literals indicate to sort in ascending (i.e., increasing) order. The last two literals indicate to sort in descending (i.e., decreasing) order. |
| 69 | + |
| 70 | +```javascript |
| 71 | +var array = require( '@stdlib/ndarray/array' ); |
| 72 | + |
| 73 | +var x = array( [ -1.0, 2.0, -3.0 ] ); |
| 74 | + |
| 75 | +// Sort in ascending order: |
| 76 | +var y = toSorted( x, 'asc' ); |
| 77 | +// returns <ndarray>[ -3.0, -1.0, 2.0 ] |
| 78 | + |
| 79 | +// Sort in descending order: |
| 80 | +y = toSorted( x, 'descending' ); |
| 81 | +// returns <ndarray>[ 2.0, -1.0, -3.0 ] |
| 82 | +``` |
| 83 | + |
| 84 | +By default, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform the operation over specific dimensions, provide a `dims` option. |
| 85 | + |
| 86 | +```javascript |
| 87 | +var array = require( '@stdlib/ndarray/array' ); |
| 88 | + |
| 89 | +var x = array( [ -1.0, 2.0, -3.0, 4.0 ], { |
| 90 | + 'shape': [ 2, 2 ], |
| 91 | + 'order': 'row-major' |
| 92 | +}); |
| 93 | + |
| 94 | +var y = toSorted( x, { |
| 95 | + 'dims': [ 0 ] |
| 96 | +}); |
| 97 | +// returns <ndarray>[ [ -3.0, 2.0 ], [ -1.0, 4.0 ] ] |
| 98 | +``` |
| 99 | + |
| 100 | +To specify the output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes], provide a `dtype` option. |
| 101 | + |
| 102 | +```javascript |
| 103 | +var array = require( '@stdlib/ndarray/array' ); |
| 104 | + |
| 105 | +var x = array( [ -1.0, 2.0, -3.0 ] ); |
| 106 | + |
| 107 | +var y = toSorted( x, { |
| 108 | + 'dtype': 'float32' |
| 109 | +}); |
| 110 | +// returns <ndarray>[ -3.0, -1.0, 2.0 ] |
| 111 | +``` |
| 112 | + |
| 113 | +#### toSorted.assign( x\[, sortOrder], out\[, options] ) |
| 114 | + |
| 115 | +Sorts the elements of an input [ndarray][@stdlib/ndarray/ctor] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions and assigns the results to an output [ndarray][@stdlib/ndarray/ctor]. |
| 116 | + |
| 117 | +```javascript |
| 118 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 119 | +var array = require( '@stdlib/ndarray/array' ); |
| 120 | + |
| 121 | +var x = array( [ -1.0, 2.0, -3.0 ] ); |
| 122 | +var y = zeros( [ 3 ] ); |
| 123 | + |
| 124 | +var out = toSorted.assign( x, y ); |
| 125 | +// returns <ndarray>[ -3.0, -1.0, 2.0 ] |
| 126 | + |
| 127 | +var bool = ( y === out ); |
| 128 | +// returns true |
| 129 | +``` |
| 130 | + |
| 131 | +The function has the following parameters: |
| 132 | + |
| 133 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. |
| 134 | +- **sortOrder**: sort order (_optional_). May be either a scalar value, string, or an [ndarray][@stdlib/ndarray/ctor] having a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] sort order must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] sort order must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. By default, the sort order is `1` (i.e., increasing order). |
| 135 | +- **out**: output [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. |
| 136 | +- **options**: function options (_optional_). |
| 137 | + |
| 138 | +The function accepts the following options: |
| 139 | + |
| 140 | +- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. |
| 141 | + |
| 142 | +</section> |
| 143 | + |
| 144 | +<!-- /.usage --> |
| 145 | + |
| 146 | +<section class="notes"> |
| 147 | + |
| 148 | +## Notes |
| 149 | + |
| 150 | +- If `sortOrder < 0.0` or is either `'desc'` or `'descending'`, the input [ndarray][@stdlib/ndarray/ctor] is sorted in **decreasing** order. If `sortOrder > 0.0` or is either `'asc'` or `'ascending'`, the input [ndarray][@stdlib/ndarray/ctor] is sorted in **increasing** order. If `sortOrder == 0.0`, the input [ndarray][@stdlib/ndarray/ctor] is left unchanged. |
| 151 | +- The algorithm distinguishes between `-0` and `+0`. When sorted in increasing order, `-0` is sorted before `+0`. When sorted in decreasing order, `-0` is sorted after `+0`. |
| 152 | +- The algorithm sorts `NaN` values to the end. When sorted in increasing order, `NaN` values are sorted last. When sorted in decreasing order, `NaN` values are sorted first. |
| 153 | +- The function iterates over [ndarray][@stdlib/ndarray/ctor] elements according to the memory layout of the input [ndarray][@stdlib/ndarray/ctor]. Accordingly, performance degradation is possible when operating over multiple dimensions of a large non-contiguous multi-dimensional input [ndarray][@stdlib/ndarray/ctor]. In such scenarios, one may want to copy an input [ndarray][@stdlib/ndarray/ctor] to contiguous memory before sorting. |
| 154 | + |
| 155 | +</section> |
| 156 | + |
| 157 | +<!-- /.notes --> |
| 158 | + |
| 159 | +<section class="examples"> |
| 160 | + |
| 161 | +## Examples |
| 162 | + |
| 163 | +<!-- eslint no-undef: "error" --> |
| 164 | + |
| 165 | +```javascript |
| 166 | +var discreteUniform = require( '@stdlib/random/discrete-uniform' ); |
| 167 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 168 | +var toSorted = require( '@stdlib/blas/ext/to-sorted' ); |
| 169 | + |
| 170 | +// Generate an ndarray of random numbers: |
| 171 | +var x = discreteUniform( [ 5, 5 ], -20, 20, { |
| 172 | + 'dtype': 'generic' |
| 173 | +}); |
| 174 | +console.log( ndarray2array( x ) ); |
| 175 | + |
| 176 | +// Perform operation: |
| 177 | +var out = toSorted( x, { |
| 178 | + 'dims': [ 0 ] |
| 179 | +}); |
| 180 | + |
| 181 | +// Print the results: |
| 182 | +console.log( ndarray2array( out ) ); |
| 183 | +``` |
| 184 | + |
| 185 | +</section> |
| 186 | + |
| 187 | +<!-- /.examples --> |
| 188 | + |
| 189 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 190 | + |
| 191 | +<section class="related"> |
| 192 | + |
| 193 | +</section> |
| 194 | + |
| 195 | +<!-- /.related --> |
| 196 | + |
| 197 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 198 | + |
| 199 | +<section class="links"> |
| 200 | + |
| 201 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/ndarray-ctor |
| 202 | + |
| 203 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/ndarray-dtypes |
| 204 | + |
| 205 | +[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/ndarray-base-broadcast-shapes |
| 206 | + |
| 207 | +</section> |
| 208 | + |
| 209 | +<!-- /.links --> |
0 commit comments