sort

Syntax

sort(X, [ascending=true])

Please refer to sort!. The only difference between sort and sort! is that the latter assigns the result to X and thus changing the value of X after the execution.

Parameters

X is a vector/matrix.

ascending (optional) is a Boolean scalar indicating whether to sort X in ascending order or descending order. The default value is true (ascending order).

Details

Return a sorted vector/matrix in ascending/descending order.

DolphinDB sort differs from NumPy numpy.sort in the following aspects:

  • Sorting order: DolphinDB sort supports both ascending and descending order through the ascending parameter. In contrast, numpy.sort only provides ascending sorting by default and does not support descending order through a parameter; descending order is typically achieved by reversing the sorted result.
  • Null value handling: DolphinDB sort treats null values as the smallest values, and their positions depend on the ascending setting. In contrast, in numpy.sort, NaN values are ordered in a fixed manner (typically placed at the end).

Examples

x=9 1 5;
x;
// output: [9,1,5]

y=sort(x);
y;
// output: [1,5,9]

sort(x, false);
// output: [9,5,1]

x=1 4 2 5 6 3$2:3;
x;
#0 #1 #2
1 2 6
4 5 3
sort x;
#0 #1 #2
1 3 5
2 4 6

The sort! function change the value of input after sorting.

x=9 1 5;
sort!(x);
x;
// output: [1 5 9];

Related function: isort