Sorts the contents of a Collection or an array.

<dsp:sort var-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

var

Names an EL variable. When you use var, you can set its scope attribute to page, request, session, or application. The default scope is page. For more information, see EL Variable Scopes.

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 enable you to access the sorted output as a different data type. For more information, see Usage Notes below.

id

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:

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:

The page processor sorts the contents of the specified item as follows, in descending order of precedence:

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

sortedArray

Renders the sorted item as an array.

sortedBooleanArray

Renders the sorted item as an array of Boolean objects.

sortedByteArray

Renders the sorted item as an array of Byte objects.

sortedCharArray

Renders the sorted item as an array of Char objects.

sortedCollection

Renders the sorted item as a Collection.

sortedDoubleArray

Renders the sorted item as an array of Double objects.

sortedEnumeration

Renders the sorted item as an Enumeration.

sortedFloatArray

Renders the sorted item as an array of Float objects.

sortedIntArray

Renders the sorted item as an array of Int objects.

sortedIterator

Renders the sorted item as an Iterator.

sortedLongArray

Renders the sorted item as an Array of Long objects.

sortedMap

Renders the sorted item as a Map.

sortedShortArray

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:

Original data type

Accessed as this data type

Example

Array of primitive values

Collection
Iterator
Enumeration
Map
array of objects

array of Strings > Collection:

Collection of String objects

Collection
array

Map

Enumeration of Longs > Map:

Map with key entries that are Long objects

Map

Collection
Iterator
Enumeration
Map
array of objects

Map > Collection:

Collection composed of Map key values

Collection
Iterator
Enumeration
Map
array of objects

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.


Copyright © 1997, 2015 Oracle and/or its affiliates. All rights reserved. Legal Notices