Skip to main content
Skip table of contents

sortCollection

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

Collections may contain various kinds of items, such as Maps, and so this command cannot automatically know how the items should be sorted. This needs to be told through the comparableValue attribute. The attribute's value should be an EL-expression that is evaluated in the context of the Collection; the result of this evaluation for each item should be a value that can be compared with other items' values (e.g. Strings can be 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 comparableValue expression to achieve this task would be ${item.Name}. The expression format is much like an expression 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 is just ${item}.  

Note that values of different types (e.g. Strings and Numbers) cannot be compared with each other. Attempting to 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 sorted Collection.

value

Required

Value type

EL-evaluated

Yes

Collection

Yes

The resolved value of this attribute is the Collection that is to be 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 each item in the Collection. The Collection is then sorted based on how these values get sorted. The expression 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 of items 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 Name of 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 comparableValue attribute 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.