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::sort指定したフィールドで結果をソートする

説明

public MongoCursor MongoCursor::sort ( array $fields )

パラメータ

fields

ソートするフィールドの配列。配列のキーがフィールド名、そして値には昇順なら 1、降順なら -1 を指定します。

個々の結果は、まず配列内の最初のフィールドでソートされ、次に (もしあれば) 二番目のフィールドでソートされ…というようになります。つまり、 fields 配列でのフィールドを指定する順番が重要になるということです。 サンプルも参照ください。

返り値

このカーソルを返します。

エラー / 例外

このカーソルの反復処理が始まっている場合に MongoCursorException をスローします。

例1 MongoCursor::sort() の例

<?php
// x の昇順でソートします
$cursor->sort(array('x' => 1));

// 連想配列内での並び順は重要です。
// たとえば次のふたつの結果は異なる可能性があります

// date の昇順、age の降順でソートします
$cursor->sort(array('date' => 1'age' => -1));

// age の降順、date の昇順でソートします
$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 | stats | sitemap | contact | advertising | mirror sites