Sorting and Grouping Maps Built-ins ¶
Common
Built-ins provide general functionality for:
-
Sorting and grouping lists of maps
-
PDF Form filling
The section below contains syntax and examples for sorting and grouping lists of maps. Documentation is also available for [PDF Form Filling].
Sorting List of Maps ¶
The map sorting function provides the ability to sort lists of maps by a common key so that they are ordered as required. Additionally, the list's sorting can be specified as ascending or descending.
Syntax
1 | Common.sortListOfMaps(<listOfMaps List<Map<Any, Any>>>, <sortByKeyInOrder Map<Any, Boolean>>) |
Parameters
-
listOfMaps - The list of maps to be sorted
- Values in the maps must be comparable (for example string, date, number or boolean), otherwise an exception will be thrown
-
sortByKeyInOrder - Sorts the list of maps passed to the function and does not return any other value
- The keys must match the keys in the list's maps
- The values of the key/value pair must be boolean, with
true
sorting in ascending order andfalse
sorting in descending order
Example
1 2 3 4 5 6 7 | stepdef "sort test" list := [{"key1" : 10, "key2" : 20}, {"key1" : 20, "key2" : 0}, {"key1" : 0, "key2" : 50}, {"key1" : 0, "key2" : 25}] Common.sortListOfMaps(list, {"key1" : false}) end |
Results in:
1 2 3 4 5 | list == [{"key1" : 20, "key2" : 0}, {"key1" : 10, "key2" : 20}, {"key1" : 0, "key2" : 50}, {"key1" : 0, "key2" : 25}] end |
Group Lists of Maps ¶
The map grouping function provides an SQL-like group-by functionality with the ability to aggregate all other key-value entries. The groupListOfMaps
function creates a temporary object on which an aggregate logic must be applied to retrieve results.
Syntax
1 2 | <result List<Map<Any, Any>> := Common.groupListOfMaps(<listOfMaps List<Map<Any, Any>>>, <groupingKeys List<Any>>) .aggregate(<aggregateFunctionMap Map<any, String>>) |
Parameters
-
listOfMaps - The list of maps to be grouped
- Their values do not need to be comparable if proper aggregate functions are applied
-
groupingKeys - The list of keys on which maps must be grouped
- If the list of grouping keys has more than one entry, the maps will be grouped together if the corresponding keys' values between these maps are equal
-
aggregateFunctionMap - The map of aggregate functions
- For this function, the object is the key on which the aggregation must be performed
- The string is the name of the aggregate function
- It must be one of the following:
- LIST - Makes a list of grouped values
- If only one map was grouped, a list for one value will be created
- It must be one of the following:
- COUNT - Maps the key to the amount of found grouped maps
- SUM - Maps the key to the sum of grouped integers
- MAX - Maps the key to the maximum value of grouped values (values must be comparable)
- MIN - Maps the key to the minimum value of grouped values (values must be comparable)
Note:
The aggregate
function should map remaining keys (although not necessarily all) after the grouping function has been called.
Example
1 2 3 4 5 6 7 8 9 10 11 | stepdef "group test" list := [{"key1" : 10, "key2" : 20}, {"key1" : 20, "key2" : 0}, {"key1" : 0, "key2" : 50}, {"key1" : 0, "key2" : 25}] resultingList := Common.groupListOfMaps(list, ["key1"]) .aggregate({"key2" : "SUM"}) println("resultingList(0)[key2] = " + resultingList[0]["key2"]) // 20 println("resultingList(1)[key2] = " + resultingList[1]["key2"]) // 0 println("resultingList(2)[key2] = " + resultingList[2]["key2"]) // 75 end |