Ordering collections and objects
To sort a series of data, 4D compares each value against the others by applying comparison criteria defined according to the data type (see sorting rules). This process relies on a sorting algorithm that establishes a total order across all elements. When all data belongs to the same data type, the comparison rules are straightforward and well-defined.
However, collections and objects, including entity selections, can contain elements and attributes of heterogeneous types: scalar types (text, numbers, booleans, dates) or complex types (objects, blobs, collections). When ordering a collection or object containing heterogeneous values, 4D applies a stratified sorting scheme that first partitions elements by type, then applies comparison rules within each type partition.
Ordering functions
The 4D language provides several mechanisms that rely on sorting collection elements, object attributes, or orchestrate sorting to produce an ordered result:
- Collection sorting functions:
collection.multiSort()(multi-criteria sorting with explicit key and order specification),collection.orderBy()(sorting by evaluating an expression on each element),collection.sort()(in-place sorting according to the natural ordering relation), - Entity selection sorting functions:
entitySelection.orderBy(), which applies the same sorting rules as collections, - Query functions with ordering:
entitySelection.query(),dataClass.query()with theorder by attributePathkeyword, which return results in deterministic order, - Order-dependent statistical functions:
collection.max(),collection.min(),entitySelection.max(),entitySelection.min(), which rely on the ordering relation to identify extrema, ORDER BY ATTRIBUTEcommand to order a database table based upon an object field.
Sorting rules
When a collection or entity selection containing elements of different types is sorted, a type-based stratification is applied according to the following algorithm:
- Partitioning phase: Elements are grouped into equivalence classes based on their base type. This phase establishes a partition of the entire element set.
- Intra-class ordering phase: Within each class, elements are sorted according to type-specific comparison rules. The default order is ascending.
Types are ordered according to the following sequence, with their respective comparison relations in ascending order:
| Rank | 型 | Also includes | Comparison rule |
|---|---|---|---|
| 1 | null | pointers (null pointers only for collections) | no comparison criteria applicable |
| 2 | boolean | logical ordering: false before true | |
| 3 | string | lexicographical order (e.g., "a" before "ab" before "b") | |
| 4 | number | time (converted to milliseconds or seconds depending on the Time inside objects database setting) | standard algebraic order (numeric comparison) |
| 5 | オブジェクト | blobs, pictures, non-null pointers (collections) | internal order (consistent for collection functions, see below) |
| 6 | collection | internal order (consistent for collection functions, see below) | |
| 7 | 日付 | chronological order (older dates before newer dates, e.g., !1990-01-01! before !2000-01-01!) |
Special numeric values
Special floating-point values +INF (positive infinity), -INF (negative infinity), and NaN (Not-a-Number) present in collections and objects are ordered according to the following natural sequence: NaN < -INF < finite values < +INF.
Consistent ordering in collections
Collection sorting functions (see Ordering functions section above) implement a consistent sort for complex types such as objects and collections. By "consistent", we mean that successive calls to the same sorting function (e.g., collection.orderBy()) on the same collection produce identical ordering for complex type values. Formally, if a sort expression yields the same comparative result for two elements, the relative order of those elements is preserved.
Other 4D sorting operations do not provide this stability guarantee when comparing complex types.