<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Sorts the results by given fields</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongocursor.snapshot.html">MongoCursor::snapshot</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocursor.tailable.html">MongoCursor::tailable</a></div> <div class="up"><a href="class.mongocursor.html">MongoCursor</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="mongocursor.sort" class="refentry"> <div class="refnamediv"> <h1 class="refname">MongoCursor::sort</h1> <p class="verinfo">(PECL mongo >=0.9.0)</p><p class="refpurpose"><span class="refname">MongoCursor::sort</span> — <span class="dc-title">Sorts the results by given fields</span></p> </div> <div class="refsect1 description" id="refsect1-mongocursor.sort-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type"><a href="class.mongocursor.html" class="type MongoCursor">MongoCursor</a></span> <span class="methodname"><strong>MongoCursor::sort</strong></span> ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$fields</code></span> )</div> </div> <div class="refsect1 parameters" id="refsect1-mongocursor.sort-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"> <em><code class="parameter">fields</code></em> </span> <dd> <p class="para"> An array of fields by which to sort. Each element in the array has as key the field name, and as value either <em>1</em> for ascending sort, or <em>-1</em> for descending sort. </p> <p class="para"> Each result is first sorted on the first field in the array, then (if it exists) on the second field in the array, etc. This means that the order of the fields in the <em><code class="parameter">fields</code></em> array is important. See also the examples section. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-mongocursor.sort-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the same cursor that this method was called on. </p> </div> <div class="refsect1 errors" id="refsect1-mongocursor.sort-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> Throws <a href="class.mongocursorexception.html" class="classname">MongoCursorException</a> if this cursor has started iterating. </p> </div> <div class="refsect1 examples" id="refsect1-mongocursor.sort-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-1486"> <p><strong>Example #1 <span class="function"><strong>MongoCursor::sort()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Sort on field x, ascending<br /></span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">sort</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'x' </span><span style="color: #007700">=> </span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// The order in the associative array is important. For instance, these two<br />// examples will yield different results:<br /><br />// Sort on date ascending and age descending<br /></span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">sort</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'date' </span><span style="color: #007700">=> </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">'age' </span><span style="color: #007700">=> -</span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Sort on age descending and date ascending<br /></span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">sort</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'age' </span><span style="color: #007700">=> -</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">'date' </span><span style="color: #007700">=> </span><span style="color: #0000BB">1</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongocursor.snapshot.html">MongoCursor::snapshot</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocursor.tailable.html">MongoCursor::tailable</a></div> <div class="up"><a href="class.mongocursor.html">MongoCursor</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>