Issue #00005915

Added CollectionResult.groupMap()

This new method groupMap() is available in all CollectionResult objects (like QueryResult and NavigatorResult).

CollectionResult.groupMap(function[, desc])

This method returns a sorted map keyed by the return value of a user defined function where its value is a list of TMLContext objects evaluating to the same key.

If the return value of the function is null, the entry is ignored and not contained in the resulting map. This feature can be used to filter entries.

The optional boolean parameter "desc" can be used to retrieve a reversed entry set.


    return doc.MODIFIED.year + 1900

The map in this example is keyed by the creation year of the documents.

This new method can be used as alternative for <tml:goupchange> using nested <tml:foreach>s. (See also #00005509: iterating through entrysets)


    _map = WGA.nav("name:home").children().groupMap(function(doc){
        return doc.MODIFIED.year + 1900
    }, true)
<tml:foreach item="_map" currentvalue="_entry">
    <h2><tml:item name="_entry.key" format="0"/></h2>
    <tml:foreach item="_entry.value" wrap="ul">
        <li><tml:meta name="title"/></li>

Returns something like


The equivalence using <tml:groupchange> is not easy to produce because you carefully have to maintain your <ul>s ...

<tml:foreach ... sortexpression="MODIFIED" sortorder="descending">

    <tml:groupchange id="gc" expression="MODIFIED.year">
        <tml:case isfirstloop="false">
        <h2><tml:meta name="MODIFIED" format="yyyy"/></h2>


    <tml:case islastloop="true">



To generate this group-map the system has to iterate over the complete result set. This therefore should not be used with large result sets.