dsp:sort
lets you organize the contents of a Collection or an array in a particular order. Items with the following data types can be used with this tag:
Array of objects
Array of primitives
Collection
Iterator
Enumeration
Map
The order applied to the items you want sorted is determined by the first of these mechanisms discovered by the page processor .
When the comparator attribute is set to a
java.util.Comparator
object, thedsp:sort
tag applies the sort instructions defined in theComparator
object.If a comparator is not specified,
dsp:sort
reads nesteddsp:orderBy
tags for ordering information.Otherwise,
dsp:sort
interprets the items in their natural order, when all items are of typejava.lang.Comparable
.
By default, the values are sorted into an object named by var
-specified EL variable that is the same data type as the values themselves. For example, sorting an array of strings as just that (an array of strings) produces a sorted array of Strings. You might want to retrieve values as a different data type. For example, you might want to render a Collection as a Map by accessing the sortedMap
property through the EL variable. The resultant Map holds the items that make up the Collection as keys. Keep in mind that rendering an item as a different data type slows site performance.
Here is the behavior you can expect when you convert items from one data type to another:
OriginalDataType | AccessedasthisDataType | ExampleResultantDataTypes |
---|---|---|
Array of primitive values Example: array of strings | Collection, Iterator, Enumeration, Map or array of objects Example: Collection | Collection holding String objects |
Collection or array Example: Enumeration of longs | Map | Map with key entries that are Long objects |
Map | Collection, Iterator, Enumeration, Map or array of objects Example: Collection | Collection made up of the values that were the Map’s keys |
Collection, Iterator, Enumeration, Map or array of objects Example: Iterator | Array of primitives | Null value |
Array of primitives Example: array of ints | Array of primitives of a different data type Example: array of shorts | Array of Short objects, which are truncated versions of the original int values |
Array of primitives Example: array of ints | Array of primitives of a different data type Example: array of longs | Array of Long objects |
Attributes
TagVariable:varorid(Required)
You must define an EL variable or scripting variable:
Attribute | Description |
---|---|
| Names an EL variable. When you use |
| Names a scripting variable, which scriptlets and expressions can access at runtime. |
VariableScope:scope
The scope
attribute determines what resources can access the named EL variable. Options include: page
, request
, session
, or application
. Omitting this attribute causes the default, page
, to be used.
ItemtoBeSearched:values(Required)
The values
attribute identifies the item with contents you want to sort. This attribute permits items of the following data types: array of primitives, array of objects, Enumeration, Iterator, Collection and Map. Keep in mind that this attribute must not be set to a Dynamic Bean or any items with Dynamic Bean properties that is used for sorting.
InstanceofComparatorObject:comparator
The comparator
attribute is set to an instance of java.util.Comparator
that you define. You use a comparator to define how to sort elements in the first item in the values
attribute.
SortingintheOppositeOrder:reverse
The reverse
attribute determines, after the item has been sorted, whether order should remain as is (false) or should be reversed (true). When omitted, the default, false, is used.
Properties
sortedArray
The sortedArray
property renders the sorted item as an array. This property is populated by a call to getSortedArray
.
sortedCollection
The sortedCollection
property renders the sorted item as a Collection. This property is populated by a call to getSortedCollection
.
sortedMap
The sortedMap
property renders the sorted item as a Map. This property is populated by a call to getSortedMap
.
sortedEnumeration
The sortedEnumeration
property renders the sorted item as an Enumeration. This property is populated by a call to getSortedEnnumerattion
.
sortedIterator
The sortedIterator
property renders the sorted item as an Iterator. This property is populated by a call to getSortedIterator
.
sortedBooleanArray
The sortedBooleanArray
property renders the sorted item as an array of Boolean objects. This property is populated by a call to getSortedBooleanArray
.
sortedByteArray
The sortedByteArray
property renders the sorted item as an array of Byte objects. This property is populated by a call to getSortedByteArray
.
sortedCharArray
The sortedCharArray
property renders the sorted item as an array of Char objects. This property is populated by a call to getSortedCharArray
.
sortedShortArray
The sortedShortArray
property renders the sorted item as an array of Short objects. This property is populated by a call to getSortedShortArray
.
sortedIntArray
The sortedIntArray
property renders the sorted item as an array of Int objects. This property is populated by a call to getIntArray
.
sortedLongArray
The sortedLongArray
property renders the sorted item as an Array of Long objects. This property is populated by a call to getSortedLongArray
.
sortedFloatArray
The sortedFloatArray
property renders the sorted item as an array of Float objects. This property is populated by a call to getSortedFloatArray
.
sortedDoubleArray
The sortedDoubleArray
property renders the sorted item as an array of Double objects. This property is populated by a call to getSortedDoubleArray
.
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.