Jump to main content Jump to doc navigation

xPDOQuery::sortby

Add an ORDER BY clause to the query.

Syntax

API Docs: xPDOQuery::sortby()

xPDOQuery sortby (string $column, [string $direction = 'ASC'])

Example

Get all the Box objects sorted by name.

$query = $xpdo->newQuery('Box');
$query->sortby('name','ASC');
$boxes = $xpdo->getCollection('Box',$query);

You can sort by a random order by referencing 'RAND()':

$query = $xpdo->newQuery('Box');
$query->sortby('RAND()');
$boxes = $xpdo->getCollection('Box',$query);

Likewise, you can pass any valid database function to the sortby method, e.g. 'FIELD()' to dictate a specific order for your results:

$query = $xpdo->newQuery('modResource');
$query->sortby('FIELD(modResource.id, 4,7,2,5,1 )');
$boxes = $xpdo->getCollection('modResource',$query);

See Also

Support the team building MODX with a monthly donation.

The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.

Backers

  • modmore
  • STERC
  • Digital Penguin
  • Jens Wittmann – Gestaltung & Entwicklung
  • Fabian Christen
  • Dannevang Digital
  • Sepia River Studios
  • Chris Fickling
  • CrewMark
  • deJaya
  • Lefthandmedia
  • Murray Wood
  • eydolan
  • Following Sea
  • Anton Tarasov
  • Stéphane Jäggi
  • Raffy
  • Snow Creative
  • A. Moreno
  • Nick Clark
  • JT Skaggs
  • Helen
  • YJ
  • krisznet
  • Richard
  • Yanni

Budget

$301 per month—let's make that $500!

Learn more