Saltar al contenido principal
Versión: Siguiente

Ordenando colecciones y objetos

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). Este proceso se basa en un algoritmo de ordenación que establece un orden total entre todos los elementos. 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.

Funciones de ordenación

The 4D language provides several mechanisms that rely on sorting collection elements, object attributes, or orchestrate sorting to produce an ordered result:

Reglas de ordenación

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. Fase de reparto: los elementos se agrupan en clases de equivalencia en función de su tipo base. Esta fase establece una partición de todo el conjunto de elementos.
  2. Intra-class ordering phase: Within each class, elements are sorted according to type-specific comparison rules. The default order is ascending.

Los tipos se ordenan según la secuencia siguiente, con sus respectivas relaciones de comparación en orden ascendente:

RankTipoTambién incluyeComparison rule
1nullpunteros (punteros null sólo para colecciones)no se aplican criterios de comparación
2booleanorden lógico: false antes que true
3stringorden lexicográfico (por ejemplo, "a" antes "ab" antes "b")
4numbertime (converted to milliseconds or seconds depending on the Time inside objects database setting)standard algebraic order (numeric comparison)
5objectblobs, imágenes, punteros no nulos (colecciones)orden interno (coherente para las funciones de collection, ver más abajo)
6collectionorden interno (coherente para las funciones de collection, ver más abajo)
7dateorden cronológico (fechas más antiguas antes de las más recientes, por ejemplo, ¡1990-01-01! before !2000-01-01!)

Valores numéricos especiales

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.

Ordenación coherente de las colecciones

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.