TapHome

Sorting

TapHome script sorting functions — ORDER, ORDERINDEX and their STRICT and descending variants for sorting values or returning sort-order indexes.

The ORDER functions sort values and return a new collection — either the sorted values or, in the ORDERINDEX variants, the original zero-based positions of the values in sorted order.

FunctionOrderReturnsUnknown values (Null, NaN)
ORDERascendingvaluesremoved
ORDERSTRICTascendingvalueskept, placed first
ORDERDESCdescendingvaluesremoved
ORDERDESCSTRICTdescendingvalueskept, placed first
ORDERINDEXascendingindexesremoved
ORDERINDEXSTRICTascendingindexeskept, placed first
ORDERINDEXDESCdescendingindexesremoved
ORDERINDEXDESCSTRICTdescendingindexeskept, placed first

Rules shared by all eight functions:

  • They take 1 to 100 values, or exactly one collection. The source collection is not changed.
  • The values must be comparable and of one kind — numbers, characters, strings, datetime or timespan; mixing numbers and strings is an error.
  • Strings and characters are sorted by character code, so uppercase letters come before lowercase ones.
  • Equal values keep their original order.
  • The STRICT variants place unknown values first, in the descending variants too.
  • An empty collection is an error: Collection is null or empty.

Example — the cheapest hour from a list of prices

VAR prices := {12.5, 8.1, 20.0, 9.9};
VAR cheapest := GETAT(ORDER(prices), 0);
# cheapest = 8.1
VAR cheapestHour := GETAT(ORDERINDEX(prices), 0);
# cheapestHour = 1

ORDER

ORDER returns the values sorted in ascending order, without Null and NaN.

1
2
ORDER(value1, value2, ...)
ORDER(collection)
ORDER(3, 1, 2);
# Result: {1, 2, 3}
ORDER(3, NaN, 2, NaN);
# Result: {2, 3}
ORDER('Z', 'a', 'X');
# Result: {'X', 'Z', 'a'}
ORDER("str1", NULL, "STR2", "stR1");
# Result: {"STR2", "stR1", "str1"}
ORDER(NULL, TIMESPAN(0, 0, 0, 0, 100), TIMESPAN(0, 0, 0, 0, 99));
# Result: {TIMESPAN(0, 0, 0, 0, 99), TIMESPAN(0, 0, 0, 0, 100)}

ORDERSTRICT

ORDERSTRICT returns the values sorted in ascending order and keeps Null and NaN at the beginning.

1
2
ORDERSTRICT(value1, value2, ...)
ORDERSTRICT(collection)
ORDERSTRICT(3, NaN, 2, NaN);
# Result: {NaN, NaN, 2, 3}
ORDERSTRICT("str1", "STR2", "stR1");
# Result: {"STR2", "stR1", "str1"}
ORDERSTRICT(NULL, TIMESPAN(0, 0, 0, 0, 99));
# Result: {NULL, TIMESPAN(0, 0, 0, 0, 99)}

ORDERDESC

ORDERDESC returns the values sorted in descending order, without Null and NaN.

1
2
ORDERDESC(value1, value2, ...)
ORDERDESC(collection)
ORDERDESC(3, 1, 2);
# Result: {3, 2, 1}
ORDERDESC(3, NaN, 2, NaN);
# Result: {3, 2}
ORDERDESC('Z', 'a', 'X');
# Result: {'a', 'Z', 'X'}
ORDERDESC("str1", NULL, "STR2", "stR1");
# Result: {"str1", "stR1", "STR2"}

ORDERDESCSTRICT

ORDERDESCSTRICT returns the values sorted in descending order and keeps Null and NaN at the beginning.

1
2
ORDERDESCSTRICT(value1, value2, ...)
ORDERDESCSTRICT(collection)
ORDERDESCSTRICT(3, NaN, 2, NaN);
# Result: {NaN, NaN, 3, 2}
ORDERDESCSTRICT(NULL, TIMESPAN(0, 0, 0, 0, 99), TIMESPAN(0, 0, 0, 0, 100));
# Result: {NULL, TIMESPAN(0, 0, 0, 0, 100), TIMESPAN(0, 0, 0, 0, 99)}

ORDERINDEX

ORDERINDEX returns the positions of the values in ascending order of value, without the positions of Null and NaN.

1
2
ORDERINDEX(value1, value2, ...)
ORDERINDEX(collection)

Returns — collection<int> of zero-based positions in the input. The first element is the position of the smallest value.

ORDERINDEX(3, 1, 2);
# Result: {1, 2, 0}
ORDERINDEX(3, NaN, 2, NaN);
# Result: {2, 0}
ORDERINDEX("str1", NULL, "STR2", "stR1");
# Result: {2, 3, 0}

ORDERINDEXSTRICT

ORDERINDEXSTRICT returns the positions of the values in ascending order of value, with the positions of Null and NaN first.

1
2
ORDERINDEXSTRICT(value1, value2, ...)
ORDERINDEXSTRICT(collection)
ORDERINDEXSTRICT(3, 1, 2);
# Result: {1, 2, 0}
ORDERINDEXSTRICT(3, NaN, 2, NaN);
# Result: {1, 3, 2, 0}
ORDERINDEXSTRICT(NULL, TIMESPAN(0, 0, 0, 0, 99));
# Result: {0, 1}

ORDERINDEXDESC

ORDERINDEXDESC returns the positions of the values in descending order of value, without the positions of Null and NaN.

1
2
ORDERINDEXDESC(value1, value2, ...)
ORDERINDEXDESC(collection)
ORDERINDEXDESC(3, 1, 2);
# Result: {0, 2, 1}
ORDERINDEXDESC(3, NaN, 2);
# Result: {0, 2}
ORDERINDEXDESC("str1", NULL, "STR2", "stR1");
# Result: {0, 3, 2}

ORDERINDEXDESCSTRICT

ORDERINDEXDESCSTRICT returns the positions of the values in descending order of value, with the positions of Null and NaN first.

1
2
ORDERINDEXDESCSTRICT(value1, value2, ...)
ORDERINDEXDESCSTRICT(collection)
ORDERINDEXDESCSTRICT(3, 1, 2);
# Result: {0, 2, 1}
ORDERINDEXDESCSTRICT(3, NaN, 2, NaN);
# Result: {1, 3, 0, 2}
ORDERINDEXDESCSTRICT(NULL, TIMESPAN(0, 0, 0, 0, 99));
# Result: {0, 1}