blob: 61cde320311847bcffcb5a0390f5a4c653ff9efb [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. Orderings also use a SortOrder to identify how the ordering should be performed.</P>
<P>There are 4 SortOrder options:</P>
<UL>
<LI>ASCENDING (ascending order, case &ndash; or database &ndash; sensitive)</LI>
<LI>ASCENDING_INSENSITIVE (ascending order, case-insensitive)</LI>
<LI>DESCENDING (descending order, case &ndash; or database &ndash; sensitive)</LI>
<LI>DESCENDING_INSENSITIVE (descending order, case-insensitive)</LI>
</UL>
<P>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;
<SPAN class="code-keyword">import</SPAN> org.apache.cayenne.query.SortOrder;
...
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>, SortOrder.ASCENDING);
</PRE>
</DIV></DIV>
<DIV class="panelMacro"><TABLE class="infoMacro"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><IMG src="http://cayenne.apache.org/docs/3.0/images/icons/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></TD><TD><B>Legacy Information</B><BR>In Cayenne versions prior to 3.0, the addOrdering() call in the above example would appear as <TT>query.addOrdering(&quot;artistName&quot;, true)</TT>. A boolean parameter of true meant to order ascending (false descending).</TD></TR></TABLE></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-keyword">import</SPAN> org.apache.cayenne.query.SortOrder;
...
<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>, SortOrder.ASCENDING);
<SPAN class="code-comment">// orders a list
</SPAN>ordering.orderList(list);
</PRE>
</DIV></DIV>
<DIV class="panelMacro"><TABLE class="infoMacro"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><IMG src="http://cayenne.apache.org/docs/3.0/images/icons/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></TD><TD><B>Legacy Information</B><BR>In Cayenne versions prior to 3.0, the Ordering() constructor in the above example would appear as: <TT>new Ordering(&quot;artistName&quot;, true)</TT>. A boolean parameter of true meant to order ascending (false descending).</TD></TR></TABLE></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-keyword">import</SPAN> org.apache.cayenne.query.SortOrder;
...
<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>, SortOrder.ASCENDING));
orderings.add(<SPAN class="code-keyword">new</SPAN> Ordering(<SPAN class="code-quote">&quot;estimatedPrice&quot;</SPAN>, SortOrder.DESCENDING));
<SPAN class="code-comment">// orders a list aplying multiple orderings
</SPAN>Ordering.orderList(list, orderings);
</PRE>
</DIV></DIV>