In ncap there are two ways to sort.
The first is a regular sort.
This sorts ALL the elements of a variable or attribute without
regard to any dimensions.
The second method applies a sort map to a variable.
To apply this sort map the size of the variable must be exactly
divisible by the size of the sort map.
The method sort(var_in,&var_map) is overloaded.
The second optional argument is a call_by_ref variable which will hold
the sort map.
a1[$time]={10,2,3,4,6,5,7,3,4,1};
a1_sort=sort(a1);
print(a1_sort);
// 1, 2, 3, 3, 4, 4, 5, 6, 7, 10 ;
a2[$lon]={2,1,4,3};
a2_sort=sort(a2,&a2_map);
print(a2);
// 1, 2, 3, 4
print(a2_map);
// 1, 0, 3, 2 ;
If the map variable does not exist prior to the sort call, then it will
be created with the same shape as the input variable and be of type
NC_INT.
If the map variable already exists, then the only restriction is that it
be of at least the same size as the input variable.
To apply a map use dsort(var_in,var_map).
defdim("nlat",5);
a3[$lon]={2,5,3,7};
a4[$nlat,$lon]={
1, 2, 3, 4,
5, 6, 7, 8,
9,10,11,12,
13,14,15,16,
17,18,19,20};
a3_sort=sort(a3,&a3_map);
print(a3_map);
// 0, 2, 1, 3 ;
a5_sort=dsort(a5,a3_map);
print(a5_sort);
// 1, 3, 2, 4,
// 5, 7, 6, 8,
// 9,11,10,12,
// 13,15,14,16,
// 17,19,18,20 ;
a3_map2[$nlat]={4,3,0,2,1 };
a5_sort2=dsort(a5,a3_map2);
print(a5_sort2);
// 3, 5, 4, 2, 1
// 8, 10, 9,7, 6,
// 13,15,14,12,11,
// 18,20,19,17,16
As in the above example you a free to create your own mask.
To sort in descending order, use reverse() after the sort.