Skip to main content
Skip table of contents

sortCollection

Produces a new variable holding a Collection withthe contents of the specified Collection sorted.

Collections may contain various kinds of items, such as Maps, and so this command cannot automatically knowhow the items should be sorted. This needs to be told through the comparableValue attribute. Theattribute's value should be an EL-expression that is evaluated in the context of the Collection; the result ofthis evaluation for each item should be a value that can be compared with other items' values (eg. Strings canbe compared with each other to produce an alphabetical order). The value types that can be compared are Currency, Date, DateTime, Number, Percentage and String.

For example, a Collection containing Maps holding data of users could be sorted based on the users' names,which are Strings. A user's name is in its Map behind the key 'Name'. The comparableValueexpression to achieve this task would be ${item.Name}. The expression format is much like anexpression within a loop iterating through the Collection, with the iterated item's variable name being"item".            

Should the Collection contain values that are directly comparable, such as Numbers, the expression to use isjust ${item}.  

Note that values of different types (eg. Strings and Numbers) cannot be compared with each other. Attemptingto do this kind of comparison produces an error.

Attributes

var

Required

Value type

EL-evaluated

Yes

String

No

Defines the name of the variable that holds the sortedCollection.

value

Required

Value type

EL-evaluated

Yes

Collection

Yes

The resolved value of this attribute is the Collection that is tobe sorted. As the sorted Collection becomes a new value, this source Collection remains unmodified.

comparableValue

Required

Value type

EL-evaluated

Yes

String

No

Specifies the expression that is used to find a value from eachitem in the Collection. The Collection is then sorted based on how these values get sorted. Theexpression is to be formed as if it was evaluated within a loop iterating through the Collection,with "item" being the variable name for the item itself.

descending

Required

Value type

EL-evaluated

No

Boolean

Yes

If this attribute resolves into true, the order ofitems in the sorted Collection is reversed. 

Examples

A Collection of Salesforce query results can be sorted based on one of the queried fields. Such as the Nameof a Contact - the following would sort the Collection's Data items alphabetically based on the Name field:

CODE
<query var="contacts" select="SELECT Id, Name FROM Contact WHERE AccountId='${mainAccId}'"><sortCollection var="sortedContacts" value="${contacts}" comparableValue="${item.Name}">

If a Collection would contain name Strings instead of Data items, the comparableValueattribute would just need to specify the item as the items are Strings and therefore directly sortable:

CODE
<sortCollection var="sortedNames" value="${names}" comparableValue="${item}"
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.