<html> <head> <META http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>SODA Sorting</title> <link rel="stylesheet" type="text/css" href="../../../../style.css"> </head> <body> <div class="CommonContent"> <div class="CommonContentArea"> <h1>SODA Sorting</h1><script> //We attach this function to a browser object as a variable so that if multiple of these blocks are //present on the same page, then they will simply over-write eachother with the same function instead //of generating a naming collision. window.WikiCodeFormattingCopyToClipboard = function copyToClipboard(sID){ var sContent = document.getElementById(sID).innerText; if( window.clipboardData && clipboardData.setData ) { clipboardData.setData("Text", sContent); } else { alert("You must enable javascript access to your clipboard for this feature to work. Please referr to your browser documentation or Google search for instructions."); } } </script> <P>SODA query API gives you a possibility to sort any field in ascending or descending order and combine sorting of different fields. For example, let's retrieve the objects of the Pilot class <a href="../sorting_query_results.html" class="wikiLink">saved before</a>, sorting "points" field in descending order and "name" field in ascending.</P> <span name="cs_wiki_filter" csw_filters="cs"> <div class="FormattedSourceCode"><div class="fscHeader"><span class="fscFileName">SortingExample.cs: <span class="fscMemberName">GetObjectsSODA</span></span></div><div class="fscCode"><pre ID="..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63"><div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #008080;">01</span><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/None.gif" align="top"/><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> GetObjectsSODA() </span><span style="color: #008080;">02</span><span style="color: #000000;"><img id="..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_45_790_Open_Image" src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_45_790_Open_Text').style.display='none'; document.getElementById('..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_45_790_Closed_Image').style.display='inline'; document.getElementById('..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_45_790_Closed_Text').style.display='inline';"/><img id="..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_45_790_Closed_Image" src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_45_790_Closed_Text').style.display='none'; document.getElementById('..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_45_790_Open_Image').style.display='inline'; document.getElementById('..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_45_790_Open_Text').style.display='inline';"/> </span><span id="..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_45_790_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">...</span><span id="..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_45_790_Open_Text"><span style="color: #000000;">{ </span><span style="color: #008080;">03</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> IObjectContainer db </span><span style="color: #000000;">=</span><span style="color: #000000;"> Db4oFactory.OpenFile(Db4oFileName); </span><span style="color: #008080;">04</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">try</span><span style="color: #000000;"> </span><span style="color: #008080;">05</span><span style="color: #000000;"><img id="..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_145_704_Open_Image" src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_145_704_Open_Text').style.display='none'; document.getElementById('..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_145_704_Closed_Image').style.display='inline'; document.getElementById('..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_145_704_Closed_Text').style.display='inline';"/><img id="..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_145_704_Closed_Image" src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedSubBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_145_704_Closed_Text').style.display='none'; document.getElementById('..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_145_704_Open_Image').style.display='inline'; document.getElementById('..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_145_704_Open_Text').style.display='inline';"/> </span><span id="..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_145_704_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">...</span><span id="..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_145_704_Open_Text"><span style="color: #000000;">{ </span><span style="color: #008080;">06</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> IQuery query </span><span style="color: #000000;">=</span><span style="color: #000000;"> db.Query(); </span><span style="color: #008080;">07</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> query.Constrain(</span><span style="color: #0000FF;">typeof</span><span style="color: #000000;">(Pilot)); </span><span style="color: #008080;">08</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> query.Descend(</span><span style="color: #800000;">"</span><span style="color: #800000;">_name</span><span style="color: #800000;">"</span><span style="color: #000000;">).OrderAscending(); </span><span style="color: #008080;">09</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> query.Descend(</span><span style="color: #800000;">"</span><span style="color: #800000;">_points</span><span style="color: #800000;">"</span><span style="color: #000000;">).OrderDescending(); </span><span style="color: #008080;">10</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> DateTime dt1 </span><span style="color: #000000;">=</span><span style="color: #000000;"> DateTime.UtcNow; </span><span style="color: #008080;">11</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> IObjectSet result </span><span style="color: #000000;">=</span><span style="color: #000000;"> query.Execute(); </span><span style="color: #008080;">12</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> DateTime dt2 </span><span style="color: #000000;">=</span><span style="color: #000000;"> DateTime.UtcNow; </span><span style="color: #008080;">13</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> TimeSpan diff </span><span style="color: #000000;">=</span><span style="color: #000000;"> dt2 </span><span style="color: #000000;">-</span><span style="color: #000000;"> dt1; </span><span style="color: #008080;">14</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Time to query and sort with SODA: </span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> diff.TotalMilliseconds </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;"> ms.</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #008080;">15</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> ListResult(result); </span><span style="color: #008080;">16</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"/> }</span></span><span style="color: #000000;"> </span><span style="color: #008080;">17</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">finally</span><span style="color: #000000;"> </span><span style="color: #008080;">18</span><span style="color: #000000;"><img id="..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_737_780_Open_Image" src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_737_780_Open_Text').style.display='none'; document.getElementById('..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_737_780_Closed_Image').style.display='inline'; document.getElementById('..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_737_780_Closed_Text').style.display='inline';"/><img id="..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_737_780_Closed_Image" src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedSubBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_737_780_Closed_Text').style.display='none'; document.getElementById('..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_737_780_Open_Image').style.display='inline'; document.getElementById('..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_737_780_Open_Text').style.display='inline';"/> </span><span id="..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_737_780_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">...</span><span id="..SortingCS.ZipsSortingExample.Cs_SortingExample.Cs_GetObjectsSODA_369_63_737_780_Open_Text"><span style="color: #000000;">{ </span><span style="color: #008080;">19</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> db.Close(); </span><span style="color: #008080;">20</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"/> }</span></span><span style="color: #000000;"> </span><span style="color: #008080;">21</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockEnd.gif" align="top"/> }</span></span></div></pre></div></div> </span> <P><span name="cs_wiki_filter" csw_filters="vb"></P> <div class="FormattedSourceCode"><div class="fscHeader"><span class="fscFileName">SortingExample.vb: <span class="fscMemberName">GetObjectsSODA</span></span></div><div class="fscCode"><pre ID="..SortingVB.ZipsSortingExample.Vb_SortingExample.Vb_GetObjectsSODA_497_63"><div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #008080;">01</span><img id="..SortingVB.ZipsSortingExample.Vb_SortingExample.Vb_GetObjectsSODA_497_63_15_786_Open_Image" src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('..SortingVB.ZipsSortingExample.Vb_SortingExample.Vb_GetObjectsSODA_497_63_15_786_Open_Text').style.display='none'; document.getElementById('..SortingVB.ZipsSortingExample.Vb_SortingExample.Vb_GetObjectsSODA_497_63_15_786_Closed_Image').style.display='inline'; document.getElementById('..SortingVB.ZipsSortingExample.Vb_SortingExample.Vb_GetObjectsSODA_497_63_15_786_Closed_Text').style.display='inline';"/><img id="..SortingVB.ZipsSortingExample.Vb_SortingExample.Vb_GetObjectsSODA_497_63_15_786_Closed_Image" src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('..SortingVB.ZipsSortingExample.Vb_SortingExample.Vb_GetObjectsSODA_497_63_15_786_Closed_Text').style.display='none'; document.getElementById('..SortingVB.ZipsSortingExample.Vb_SortingExample.Vb_GetObjectsSODA_497_63_15_786_Open_Image').style.display='inline'; document.getElementById('..SortingVB.ZipsSortingExample.Vb_SortingExample.Vb_GetObjectsSODA_497_63_15_786_Open_Text').style.display='inline';"/><span style="color: #0000FF;">Private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Shared</span><span style="color: #000000;"> </span><span id="..SortingVB.ZipsSortingExample.Vb_SortingExample.Vb_GetObjectsSODA_497_63_15_786_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">Sub GetObjectsSODA()</span><span id="..SortingVB.ZipsSortingExample.Vb_SortingExample.Vb_GetObjectsSODA_497_63_15_786_Open_Text"><span style="color: #0000FF;">Sub</span><span style="color: #000000;"> GetObjectsSODA() </span><span style="color: #008080;">02</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> db </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> IObjectContainer </span><span style="color: #000000;">=</span><span style="color: #000000;"> Db4oFactory.OpenFile(Db4oFileName) </span><span style="color: #008080;">03</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Try</span><span style="color: #000000;"> </span><span style="color: #008080;">04</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> query </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> IQuery </span><span style="color: #000000;">=</span><span style="color: #000000;"> db.Query </span><span style="color: #008080;">05</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> query.Constrain(</span><span style="color: #0000FF;">GetType</span><span style="color: #000000;">(Pilot)) </span><span style="color: #008080;">06</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> query.Descend(</span><span style="color: #800000;">"</span><span style="color: #800000;">_name</span><span style="color: #800000;">"</span><span style="color: #000000;">).OrderAscending() </span><span style="color: #008080;">07</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> query.Descend(</span><span style="color: #800000;">"</span><span style="color: #800000;">_points</span><span style="color: #800000;">"</span><span style="color: #000000;">).OrderDescending() </span><span style="color: #008080;">08</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> dt1 </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> DateTime </span><span style="color: #000000;">=</span><span style="color: #000000;"> DateTime.UtcNow </span><span style="color: #008080;">09</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> result </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> IObjectSet </span><span style="color: #000000;">=</span><span style="color: #000000;"> query.Execute </span><span style="color: #008080;">10</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> dt2 </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> DateTime </span><span style="color: #000000;">=</span><span style="color: #000000;"> DateTime.UtcNow </span><span style="color: #008080;">11</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> diff </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> TimeSpan </span><span style="color: #000000;">=</span><span style="color: #000000;"> dt2 </span><span style="color: #000000;">-</span><span style="color: #000000;"> dt1 </span><span style="color: #008080;">12</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Time to query and sort with SODA: </span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> diff.TotalMilliseconds.ToString() </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;"> ms.</span><span style="color: #800000;">"</span><span style="color: #000000;">) </span><span style="color: #008080;">13</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> ListResult(result) </span><span style="color: #008080;">14</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Finally</span><span style="color: #000000;"> </span><span style="color: #008080;">15</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> db.Close() </span><span style="color: #008080;">16</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">End</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Try</span><span style="color: #000000;"> </span><span style="color: #008080;">17</span><span style="color: #000000;"><img src="../../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockEnd.gif" align="top"/> </span><span style="color: #0000FF;">End Sub</span></span></div></pre></div></div> <P></span><BR></P> <P>Obvious disadvantages of this method:</P> <UL> <LI>limitations of SODA queries (not type-safe and not compile-time checked);</LI> <LI>limitations if sorting mechanism (only alphabetical for strings, numerical for numbers and object id for objects)</LI></UL> <P>The valuable advantage of this method is its high performance.</P></div> </div> <div id="footer"> This revision (8) was last Modified 2007-10-13T13:39:44 by Tetyana. </div> </body> </html>