downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

MongoCursor::tailable> <MongoCursor::snapshot
[edit] Last updated: Fri, 25 May 2012

view this page in

MongoCursor::sort

(PECL mongo >=0.9.0)

MongoCursor::sortSorts the results by given fields

Description

public MongoCursor MongoCursor::sort ( array $fields )

Parameters

fields

An array of fields by which to sort. Each element in the array has as key the field name, and as value either 1 for ascending sort, or -1 for descending sort.

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 fields array is important. See also the examples section.

Return Values

Returns the same cursor that this method was called on.

Errors/Exceptions

Throws MongoCursorException if this cursor has started iterating.

Examples

Example #1 MongoCursor::sort() example

<?php
// Sort on field x, ascending
$cursor->sort(array('x' => 1));

// The order in the associative array is important. For instance, these two
// examples will yield different results:

// Sort on date ascending and age descending
$cursor->sort(array('date' => 1'age' => -1));

// Sort on age descending and date ascending
$cursor->sort(array('age' => -1'date' => 1));
?>


add a note add a note User Contributed Notes MongoCursor::sort
klgleb at gmail dot com 05-Jul-2011 11:00
Here both arrays are equal

<?php
// sort date ascending and age descending
$cursor->sort(array('date' => 1, 'age' => -1));

// sort age descending and date ascending
$cursor->sort(array('age' => -1, 'date' => 1));
?>

 
show source | credits | sitemap | contact | advertising | mirror sites