SORTWITH
lists
gts
The SORTWITH
function sorts a LIST or a MAP according to a value returned by a comparison macro. Only MAPs of type LinkedHashMap, which are the most commons in WarpScript, can be sorted.
In used a on LIST, the comparison macro is given 2 elements of the LIST and return a LONG which can be represented on 32 bits. The returned value must be negative, zero or positive if the deepest element in the stack is respectively strictly less than, equal to or strictly more than than the element on the stack.
In used a on MAP, the comparison macro is given 2 entries (key deepest, value shallowest) of the MAP and return a LONG which can be represented on 32 bits. The returned value must be negative, zero or positive if the deepest entry in the stack is respectively strictly less than, equal to or strictly more than than the shallowest entry on the stack.
Before release 2.7.0
, the SORTWITH
function could only sort lists but not maps.
SORTWITH is available since version 2.2.0.
See also
Signatures
Examples
//
// Create 3 random Geo Time Series
//
1 3
<%
NEWGTS
SWAP TOSTRING RENAME
1 100
<%
NaN NaN NaN RAND ADDVALUE
%>
FOR
%>
FOR
//
// Also add a Geo Time Series with the same mean than the last
// created GTS but with a lower std.
//
CLONE DUP false MUSIGMA DROP 'mu' STORE
'4' RENAME
101 NaN NaN NaN $mu ADDVALUE
102 NaN NaN NaN $mu ADDVALUE
4 ->LIST
// Add mean and std as attributes, only here to check the result.
<%
DROP
DUP false MUSIGMA [ 'mu' 'sigma' ] STORE
{ 'mu' $mu TOSTRING 'sigma' $sigma TOSTRING } SETATTRIBUTES
%>
LMAP
//
// Sort the GTS according to their mean and, if equal, to their standard deviation
//
<%
[ 'a' 'b' ] STORE
$a false MUSIGMA [ 'a_mu' 'a_sigma' ] STORE
$b false MUSIGMA [ 'b_mu' 'b_sigma' ] STORE
$a_mu $b_mu <
<% -1 %>
IFT
$a_mu $b_mu >
<% 1 %>
IFT
$a_mu $b_mu ==
<%
$a_sigma $b_sigma <
<% -1 %>
IFT
$a_sigma $b_sigma >
<% 1 %>
IFT
$a_sigma $b_sigma ==
<% 0 %>
IFT
%>
IFT
%>
SORTWITH
{
10 10
1 2
5 4
}
<%
[ 'k1' 'v1' 'k2' 'v2' ] STORE
$k1 $v1 * 'p1' STORE
$k2 $v2 * 'p2' STORE
$p1 $p2 <
<% -1 %>
IFT
$p1 $p2 >
<% 1 %>
IFT
$p1 $p2 ==
<% 0 %>
IFT
%>
SORTWITH