# 6.2.1 ORDER

Part of: [6 Language](6-language.md) › [6.2 Processing](6-2-processing.md)

### Syntax

**ORDER** {{**ASC** | **DESC** | **REV**} | {**,** {**ASC** | **DESC** | **REV**}} | {{**ASC** | **DESC** | **REV**}**,** {**ASC** | **DESC** | **REV**}}}

### Description

Order (i.e. sort) a result set in an ascending, descending, or reverse way by specifying either the keyword ASC, DESC, or REV, respectively. When in an ascending or descending order, HDFql automatically uses a certain number of (CPU) threads (that may have been set through the operation [SET THREAD](6-8-17-set-thread.md)) to speed up task completion[^33]. Additionally, if the result set is of data type [HDFQL_CHAR](5-1-constants.md#hdfql_char), [HDFQL_VARTINYINT](5-1-constants.md#hdfql_vartinyint), [HDFQL_UNSIGNED_VARTINYINT](5-1-constants.md#hdfql_unsigned_vartinyint), [HDFQL_VARSMALLINT](5-1-constants.md#hdfql_varsmallint), [HDFQL_UNSIGNED_VARSMALLINT](5-1-constants.md#hdfql_unsigned_varsmallint), [HDFQL_VARINT](5-1-constants.md#hdfql_varint), [HDFQL_UNSIGNED_VARINT](5-1-constants.md#hdfql_unsigned_varint), [HDFQL_VARBIGINT](5-1-constants.md#hdfql_varbigint), [HDFQL_UNSIGNED_VARBIGINT](5-1-constants.md#hdfql_unsigned_varbigint), [HDFQL_VARFLOAT](5-1-constants.md#hdfql_varfloat), [HDFQL_VARDOUBLE](5-1-constants.md#hdfql_vardouble), or [HDFQL_OPAQUE](5-1-constants.md#hdfql_opaque), the result subset can be ordered (i.e. sorted) in an ascending, descending, or reverse way by specifying a comma (,) and either the keyword ASC, DESC, or REV, respectively. Of note, when the result set is of data type [HDFQL_COMPOUND](5-1-constants.md#hdfql_compound), ordering is ignored (i.e. has no effect).

### Parameter(s)

None

### Return

The result set and/or subset is ordered (i.e. sorted) in an ascending, descending, or reverse way depending on whether the keyword ASC, DESC, or REV is specified, respectively.

### Example(s)

```hdfql
# create an HDF5 dataset named "my_dataset0" of data type float of four dimensions (size 5x8x4x7)
CREATE DATASET my_dataset0 AS FLOAT(5, 8, 4, 7)

# show (i.e. get) dimensions of dataset "my_dataset0" and populate cursor in use with these (should return 5, 8, 4, 7)
SHOW DIMENSION my_dataset0

# show (i.e. get) dimensions of dataset "my_dataset0" and populate cursor in use with these in ascending order (should return 4, 5, 7, 8)
SHOW DIMENSION my_dataset0 ORDER ASC

# show (i.e. get) dimensions of dataset "my_dataset0" and populate cursor in use with these in descending order (should return 8, 7, 5, 4)
SHOW DIMENSION my_dataset0 ORDER DESC

# show (i.e. get) dimensions of dataset "my_dataset0" and populate cursor in use with these in reversed order (should return 7, 4, 8, 5)
SHOW DIMENSION my_dataset0 ORDER REV
```

```hdfql
# create an HDF5 dataset named "my_dataset1" of data type double of two dimensions (size 3x2)
CREATE DATASET my_dataset1 AS DOUBLE(3, 2)

# insert (i.e. write) values into dataset "my_dataset1" 
INSERT INTO my_dataset1 VALUES((3.2, 1.3), (0, 0.2), (9.1, 6.5))

# select (i.e. read) data from dataset "my_dataset1" and populate cursor in use with it (should return 3.2, 1.3, 0, 0.2, 9.1, 6.5)
SELECT FROM my_dataset1

# select (i.e. read) data from dataset "my_dataset1" and populate cursor in use with it in ascending order (should return 0, 0.2, 1.3, 3.2, 6.5, 9.1)
SELECT FROM my_dataset1 ORDER ASC

# select (i.e. read) data from dataset "my_dataset1" and populate cursor in use with it in descending order (should return 9.1, 6.5, 3.2, 1.3, 0.2, 0)
SELECT FROM my_dataset1 ORDER DESC

# select (i.e. read) data from dataset "my_dataset1" and populate cursor in use with it in reversed order (should return 6.5, 9.1, 0.2, 0, 1.3, 3.2)
SELECT FROM my_dataset1 ORDER REV

# create an HDF5 dataset named "my_dataset2" of data type variable-length double of one dimension (size 3)
CREATE DATASET my_dataset2 AS VARDOUBLE(3)

# insert (i.e. write) values into dataset "my_dataset2" 
INSERT INTO my_dataset2 VALUES((3.2, 1.3), (0, 0.2), (9.1, 7.4, 6.5))

# select (i.e. read) data from dataset "my_dataset2" and populate cursor in use with it (should return 3.2, 1.3, 0, 0.2, 9.1, 7.4, 6.5)
SELECT FROM my_dataset2

# select (i.e. read) data from dataset "my_dataset2" and populate cursor in use with it in ascending order on the result subset only (should return 1.3, 3.2, 0, 0.2, 6.5, 7.4, 9.1)
SELECT FROM my_dataset2 ORDER , ASC 

# select (i.e. read) data from dataset "my_dataset2" and populate cursor in use with it in descending order on the result subset only (should return 3.2, 1.3, 0.2, 0, 9.1, 7.4, 6.5)
SELECT FROM my_dataset2 ORDER , DESC

# select (i.e. read) data from dataset "my_dataset2" and populate cursor in use with it in reversed order on the result set only (should return 9.1, 7.4, 6.5, 0, 0.2, 3.2, 1.3)
SELECT FROM my_dataset2 ORDER REV

# select (i.e. read) data from dataset "my_dataset2" and populate cursor in use with it in reversed order on the result subset only (should return 1.3, 3.2, 0.2, 0, 6.5, 7.4, 9.1)
SELECT FROM my_dataset2 ORDER , REV

# select (i.e. read) data from dataset "my_dataset2" and populate cursor in use with it in reversed order on both the result set and result subset (should return 6.5, 7.4, 9.1, 0.2, 0, 1.3, 3.2)
SELECT FROM my_dataset2 ORDER REV, REV
```

[^33]: Through a parallelized Quicksort algorithm.
