The C++ interface for sort. More...

array sort (const array &in, int dim=-1, bool isdescending=false)
 Sort array along dimension. More...
 
void sort (array &values, array &indices, const array &in, int dim=-1, bool isdescending=false)
 Sort array along dimension and return original indices of sorted values. More...
 
void sort (array &outkeys, array &outvalues, const array &keys, const array &values, bool isdescending=false)
 Sort array values based on keys along dimension. More...
 

Detailed Description

The C++ interface for sort.

Sorting an array reorders the elements in the array along the dimension specified.

The defualt order is ascending order. That is, the smallest element along the dimension is places at the first index location and the largest element is placed at the last location. To sort in descending order, set the isdescending flag to true.

The indices array parameter can be used to retrieve the indices of the elements before the sort.

array a = rand(5); //a = [6]
// [9]
// [1]
// [7]
// [4]
a = sort(a,0); //a = [1]
// [4]
// [6]
// [7]
// [9]
array a = rand(5); //a = [6]
// [9]
// [1]
// [7]
// [4]
array out,indices;
sort(out,indices,a,0);
//out,indices = [1],[2]
// [4],[4]
// [6],[0]
// [7],[3]
// [9],[1]

The next sort function can sort the values of an array (value) based on another array (key). The function first sorts the key array and uses the indices from this sort to reorder the value array.

float keys[] = {1, 6, 2, 5, 3, 4};
array value(6,1,values); // [5 10 41 2 5 7]
array key(6,1,keys); // [1 6 2 5 3 4]
array outkeys,outvalues;
sort(outkeys,outvalues,key,value);
print(outvalues); // [5 41 5 7 2 10]
print(outkeys); // [1 2 3 4 5 6]

The function sortdim uses one of the dimensions as key to sort the array.

// sort rows based on first column
array a = floor(5 * randu(5,5));
array b = sortdim(a);
array a = floor(5 * randu(5,5));
const int order[] = {3, 1};
array d = sortdim(a, 0, 2, order);

Function Documentation

array af::sort ( const array &  in,
int  dim = -1,
bool  isdescending = false 
)

Sort array along dimension.

array a = rand(5); //a = [6]
// [9]
// [1]
// [7]
// [4]
a = sort(a,0); //a = [1]
// [4]
// [6]
// [7]
// [9]
Parameters
[in]in
[in]dimdimension along which to operate (-1 indicates first non-singleton dimension)
[in]isdescendingtrue if descending order, false (default) in ascending order
Returns
sorted array
void af::sort ( array &  values,
array &  indices,
const array &  in,
int  dim = -1,
bool  isdescending = false 
)

Sort array along dimension and return original indices of sorted values.

array a = rand(5); //a = [6]
// [9]
// [1]
// [7]
// [4]
array out,indices;
sort(out,indices,a,0);
//out,indices = [1],[2]
// [4],[4]
// [6],[0]
// [7],[3]
// [9],[1]
Parameters
[out]values
[out]indices
[in]in
[in]dimdimension along which to operate (-1 indicates first non-singleton dimension)
[in]isdescendingtrue if descending order, false (default) in ascending order
void af::sort ( array &  outkeys,
array &  outvalues,
const array &  keys,
const array &  values,
bool  isdescending = false 
)

Sort array values based on keys along dimension.

float values[] = {5,10,41, 2, 5, 7};
float keys[] = {1, 6, 2, 5, 3, 4};
array value(6,1,values); // [5 10 41 2 5 7]
array key(6,1,keys); // [1 6 2 5 3 4]
array outkeys,outvalues;
sort(outkeys,outvalues,key,value);
print(outvalues); // [5 41 5 7 2 10]
print(outkeys); // [1 2 3 4 5 6]
Parameters
[out]outkeys
[out]outvalues
[in]keys
[in]values
[in]isdescendingtrue if descending order, false (default) in ascending order