OpenWGA 7.6 - TMLScript reference
WGAMethod :
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:
|
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 |
|
Examples |
Simple example of variant 1, sorting some arbitrary list of trivial objects: var sourceList = WGA.createList(); 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) { 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); |