Sorts the contents of a Collection or an array.
<dsp:sortvar-spec
values="sort-item
"[
comparator="comparator-obj
"]
[
reverse="{true|false}"]
> ... </dsp:sort>
Attributes
var-spec
An EL variable or scripting variable that stores the sorted output, defined with one of these attributes:
Attribute | Description |
---|---|
| Names an EL variable. When you use By default, the values that are sorted into the The EL variable has a number of properties that enable you to access the sorted output as a different data type. For more information, see Usage Notes below. |
| Names a scripting variable that scriptlets and expressions can access at runtime. |
values
Identifies the item to sort. This attribute can be set to items of the following data types:
array of primitives
array of objects
Enumeration
Iterator
Collection
Map
This attribute cannot be set to a Dynamic Bean or any items with Dynamic Bean properties to be used for sorting.
comparator
Set to an instance of java.util.Comparator
that you define. Use a comparator to define how to sort elements in the first item in the values
attribute.
reverse
After the item is sorted, determines whether to use the initial order (false) or reverse it (true). If you omit this attribute, the default is false.
Usage Notes
dsp:sort
sorts the contents of a Collection or an array in the specified order. You can sort items of the following data types:
Array of objects
Array of primitives
Collection
Iterator
Enumeration
Map
The page processor sorts the contents of the specified item as follows, in descending order of precedence:
If the comparator attribute is set to a
java.util.Comparator
object,dsp:sort
uses the sort instructions defined in theComparator
object.dsp:sort
uses the nesteddsp:orderBy
tags.dsp:sort
interprets the items in their natural order, when all items are of typejava.lang.Comparable
.
By default, the values that are sorted into the var
-specified object have the same data type as the source values. For example, sorting an array of strings produces another array of Strings.
The EL variable has a number of properties that provide access to the sorted output as a different data type, shown in the table below. For example, the sortedMap
property lets you render a Collection as a Map. The resultant Map holds the Collection items as keys.
Note: Rendering an item as a different data type slows site performance.
Sort variable properties
Sort property | Description |
---|---|
| Renders the sorted item as an array. |
| Renders the sorted item as an array of Boolean objects. |
| Renders the sorted item as an array of Byte objects. |
| Renders the sorted item as an array of Char objects. |
| Renders the sorted item as a Collection. |
| Renders the sorted item as an array of Double objects. |
| Renders the sorted item as an Enumeration. |
| Renders the sorted item as an array of Float objects. |
| Renders the sorted item as an array of Int objects. |
| Renders the sorted item as an Iterator. |
| Renders the sorted item as an Array of Long objects. |
| Renders the sorted item as a Map. |
| Renders the sorted item as an array of Short objects. |
The following table describes what happens when you convert items from one data type to another:
Originaldatatype | Accessedasthisdatatype | Example |
---|---|---|
Array of primitive values | Collection | array of Strings > Collection: Collection of String objects |
Collection | Map | Enumeration of Longs > Map: Map with key entries that are Long objects |
Map | Collection | Map > Collection: Collection composed of Map key values |
Collection | Array of primitives | Iterator > Array of primitives: Null value |
Array of primitives | Array of primitives of a different data type | array of ints > array of shorts: Array of short objects that are truncated versions of the original int values |
Example
<dsp:importbean bean="/atg/samples/AdvancedAlgebra" var="advAlgebra"/>
<dsp:sort var="sorter" values="${advAlgebra.students}">
<dsp:orderBy property="lastName"/>
<c:forEach var="student" begin="0" items="${sorter.sortedMapArray}">
<dsp:tomap value="${student}" var="studentMap"/>
<li>Name: <c:out value="${studentMap.lastName}"/>,
<c:out value="${studentNap.firstName}"/>
</c:forEach>
</dsp:sort>
This example organizes the student roster for Advanced Algebra. The AdvancedAlgebra
component has a Students
property that is a Map of student last names and first names. In this example, the students are organized alphabetically by last name. Then, each student’s first name and last name are displayed.