---
title: "Sorting"
description: "TapHome scripting sorting functions: ORDER, ORDERSTRICT, ORDERINDEX, ORDERINDEXSTRICT and descending variants (ORDERDESC, ORDERINDEXDESC, etc.) with NaN/NULL handling."
url: "https://taphome.com/en/docs/script/sorting/"
language: en
lastmod: 2026-11-06
generator: hugo
ai:
  topics: ["sorting","collections","ordering","scripting-language"]
  questions: ["How do I sort values in ascending order in TapHome script?","How to sort values in descending order in TapHome?","What is the difference between ORDER and ORDERINDEX?","How do STRICT sorting variants handle NaN and NULL values?","How do I get the indexes of values in sorted order?","How does TapHome handle NaN and NULL when sorting?","How do I find the cheapest hour from a list of prices?"]
---

# Sorting

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

Last updated: 2026-11-06

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, `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**

```taphome
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`.

```text
ORDER(value1, value2, ...)
ORDER(collection)
```

```taphome
# numbers, ascending
ORDER(3, 1, 2);
# Result: {1, 2, 3}
# NaN values are removed
ORDER(3, NaN, 2, NaN);
# Result: {2, 3}
# characters by character code
ORDER('Z', 'a', 'X');
# Result: {'X', 'Z', 'a'}
# strings by character code, NULL removed
ORDER("str1", NULL, "STR2", "stR1");
# Result: {"STR2", "stR1", "str1"}
# timespans, NULL removed
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.

```text
ORDERSTRICT(value1, value2, ...)
ORDERSTRICT(collection)
```

```taphome
# NaN values are kept first
ORDERSTRICT(3, NaN, 2, NaN);
# Result: {NaN, NaN, 2, 3}
# strings by character code
ORDERSTRICT("str1", "STR2", "stR1");
# Result: {"STR2", "stR1", "str1"}
# NULL is kept first
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`.

```text
ORDERDESC(value1, value2, ...)
ORDERDESC(collection)
```

```taphome
# numbers, descending
ORDERDESC(3, 1, 2);
# Result: {3, 2, 1}
# NaN values are removed
ORDERDESC(3, NaN, 2, NaN);
# Result: {3, 2}
# characters, descending by code
ORDERDESC('Z', 'a', 'X');
# Result: {'a', 'Z', 'X'}
# strings, descending, NULL removed
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.

```text
ORDERDESCSTRICT(value1, value2, ...)
ORDERDESCSTRICT(collection)
```

```taphome
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`.

```text
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.

```taphome
# positions of numbers, ascending
ORDERINDEX(3, 1, 2);
# Result: {1, 2, 0}
# positions of NaN are left out
ORDERINDEX(3, NaN, 2, NaN);
# Result: {2, 0}
# positions of strings, NULL left out
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.

```text
ORDERINDEXSTRICT(value1, value2, ...)
ORDERINDEXSTRICT(collection)
```

```taphome
# positions of numbers, ascending
ORDERINDEXSTRICT(3, 1, 2);
# Result: {1, 2, 0}
# positions of NaN come first
ORDERINDEXSTRICT(3, NaN, 2, NaN);
# Result: {1, 3, 2, 0}
# position of NULL comes first
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`.

```text
ORDERINDEXDESC(value1, value2, ...)
ORDERINDEXDESC(collection)
```

```taphome
# positions of numbers, descending
ORDERINDEXDESC(3, 1, 2);
# Result: {0, 2, 1}
# positions of NaN are left out
ORDERINDEXDESC(3, NaN, 2);
# Result: {0, 2}
# positions of strings, NULL left out
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.

```text
ORDERINDEXDESCSTRICT(value1, value2, ...)
ORDERINDEXDESCSTRICT(collection)
```

```taphome
# positions of numbers, descending
ORDERINDEXDESCSTRICT(3, 1, 2);
# Result: {0, 2, 1}
# positions of NaN come first
ORDERINDEXDESCSTRICT(3, NaN, 2, NaN);
# Result: {1, 3, 0, 2}
# position of NULL comes first
ORDERINDEXDESCSTRICT(NULL, TIMESPAN(0, 0, 0, 0, 99));
# Result: {0, 1}
```

**See also** — [Text and collections](https://taphome.com/en/docs/script/text-string-and-byte-array/), [Mathematical functions](https://taphome.com/en/docs/script/mathematical-functions/).
