blob: 176f91b45000773d3d50effa2c3dfe0d23fa0ebd [file] [log] [blame]
Title: Using Orderings
<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.apache.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.apache.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.apache.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>