OpenWGA 7.7 - TMLScript reference

WGA
Method :

sortList(list)
sortList(list, sortMeta, [order])
sortList(list, compareFunction, [order])

On object WGA
Usage Sorts a List
Description The sorting is normally performed "in-place" on the given list object. Exception: If the list is a struct entry list from the WGAPI. In that case a sorted copy of that list is returned.

There are three variants differing in the way that the wanted order is specified.

Variant 1:
Sorts the list elements by their natural sort order ascending, which is most useful with trivial objects like strings, numbers and dates. On other object types the Java comparision functionality is used, which utilizes the compareTo() on the Comparable interface when available.

Variant 2:
Can be used to sort WGAPI content objects in the list based on a content metadata field.

Variant 3:
The most flexible variant takes a JavaScript function which is able to compare those objects that are contained in the list. The function itself receives two objects as parameters and must determine in which order those objects should be. Therefor it must return a Number of integer value:
  • Return 1 or bigger if parameter object 1 should be after parameter object 2
  • Return -1 or lower if parameter object 1 should be before parameter object 2
  • Return 0 if both objects should be treated as absolutely equal (meaning that their order will be arbitrary)
For backward compatibility there still is a global function "sortList()" available whose use is discouraged since OpenWGA 5.3.
Parameters list (List):
The list to be sorted. In variant 2 must contain WGAPI document objects.

sortMeta (String):
Variant 2: Name of a content metadata field by which to sort the list.

compareFunction (Funktion):

Variant 3: A function that can compare two list objects. See description for details.

order (String, optional):

Determines sort order. Specify "up" for ascending and "down" for descending order. Defaults to "up".

Allowed in script types
  • WebTML pages and normal WebTML actions
  • Master actions
  • TMLScript tasks in jobs
  • Content type events
    portletevent
Examples Simple example of variant 1, sorting some arbitrary list of trivial objects:

var sourceList = WGA.createList();
sourceList.add("Amsterdam");
sourceList.add("Berlin");
sourceList.add("London");
WGA.sortList(sourceList);


Example of variant 2 where "docList" contains WGAPI content documents of type "WGContent". We sort them by their title:

WGA.sortList(docList, "title");



An example of variant 3. We sort "nodeList" which contains DOM4 node objects. They have a property "cell[0]" which should be used for determining the order. Therefor we create a JavaScript function which is capable of comparing those nodes based on this property. Additionally we convert the property to lowercase as we do not want sorting to use the individual case:


function compareRows(a,b) {
    var aCell = a.cell[0].toLowerCase();
    var bCell = b.cell[0].toLowerCase();
    if (aCell > bCell) {
        return 1;
    }
    else if (a.Cell < bCell) {
        return -1;
    }
    else {
        return 0;
    }
}


Using this function as compare function with sortList variant 3. Note that only the function object stored under "compareRows" is given as parameter, which is why there are no function brackets behind the reference.

WGA.sortList(nodelist, compareRows);