blob: 2ea6cfa563836c7dbc59b881290e913f2fab8c77 [file] [log] [blame]
---
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
title: Using Orderings
url: /docs/1.2/using-orderings.html
layout: docs_legacy
---
<P>To sort SelectQuery results, orderings are used. Orderings use path expressions discussed in the previous section to identify the attributes that must be used in sorting. For example to order results by artist name, the following code can be used:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java"><SPAN class="code-keyword">import</SPAN> org.objectstyle.cayenne.query.SelectQuery;
...
SelectQuery query = <SPAN class="code-keyword">new</SPAN> SelectQuery(<SPAN class="code-quote">&quot;Artist&quot;</SPAN>);
<SPAN class="code-comment">// add ordering by Artist name:
</SPAN>query.addOrdering(<SPAN class="code-quote">&quot;artistName&quot;</SPAN>, <SPAN class="code-keyword">true</SPAN>);
</PRE>
</DIV></DIV>
<P>Orderings also support in-memory sorting of lists of Java Beans (all DataObjects are normally Java Beans, since they has set/get method pairs for all the properties). For instance to sort with a single ordering, the following code might be used:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java"><SPAN class="code-keyword">import</SPAN> org.objectstyle.cayenne.query.Ordering;
...
<SPAN class="code-comment">// assume <SPAN class="code-keyword">this</SPAN> is a properly initialized list of Artists
</SPAN>List list = ...;
<SPAN class="code-comment">// creates asending ordering by Artist name
</SPAN>Ordering ordering = <SPAN class="code-keyword">new</SPAN> Ordering(<SPAN class="code-quote">&quot;artistName&quot;</SPAN>, <SPAN class="code-keyword">true</SPAN>);
<SPAN class="code-comment">// orders a list
</SPAN>ordering.orderList(list);
</PRE>
</DIV></DIV>
<P>If there is a need to sort on more than one object property, multiple Orderings can be passed as a List to a static method <TT>orderList(List, List)</TT>. The cost of adding new Orderings decreases, as the list of objects ends up being sorted by the first Ordering, then, if any two objects are equal for first Ordering, by the second, and so on.</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java"><SPAN class="code-keyword">import</SPAN> org.objectstyle.cayenne.query.Ordering;
...
<SPAN class="code-comment">// assume <SPAN class="code-keyword">this</SPAN> is a properly initialized list of Paintings
</SPAN>List list = ...;
List orderings = <SPAN class="code-keyword">new</SPAN> ArrayList();
orderings.add(<SPAN class="code-keyword">new</SPAN> Ordering(<SPAN class="code-quote">&quot;paintingTitle&quot;</SPAN>, <SPAN class="code-keyword">true</SPAN>));
orderings.add(<SPAN class="code-keyword">new</SPAN> Ordering(<SPAN class="code-quote">&quot;estimatedPrice&quot;</SPAN>, <SPAN class="code-keyword">false</SPAN>));
<SPAN class="code-comment">// orders a list aplying multiple orderings
</SPAN>Ordering.orderList(list, orderings);
</PRE>
</DIV></DIV>