Skip to main content
Version: Next

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:

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:

  1. Partitioning phase: Elements are grouped into equivalence classes based on their base type. This phase establishes a partition of the entire element set.
  2. 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:

RankTypeAlso includesComparison rule
1nullpointers (null pointers only for collections)no comparison criteria applicable
2booleanlogical ordering: false before true
3stringlexicographical order (e.g., "a" before "ab" before "b")
4numbertime (converted to milliseconds or seconds depending on the Time inside objects database setting)standard algebraic order (numeric comparison)
5objectblobs, pictures, non-null pointers (collections)internal order (consistent for collection functions, see below)
6collectioninternal order (consistent for collection functions, see below)
7datechronological 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.