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.
| Function | Order | Returns | Unknown values (Null, NaN) |
|---|---|---|---|
ORDER | ascending | values | removed |
ORDERSTRICT | ascending | values | kept, placed first |
ORDERDESC | descending | values | removed |
ORDERDESCSTRICT | descending | values | kept, placed first |
ORDERINDEX | ascending | indexes | removed |
ORDERINDEXSTRICT | ascending | indexes | kept, placed first |
ORDERINDEXDESC | descending | indexes | removed |
ORDERINDEXDESCSTRICT | descending | indexes | kept, 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,
datetimeortimespan; 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
STRICTvariants 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 = 1ORDER
ORDER returns the values sorted in ascending order, without Null and NaN.
| |
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.
| |
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.
| |
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.
| |
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.
| |
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.
| |
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.
| |
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.
| |
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}