Firestore/src/Query.php
public function orderBy($fieldPath, $direction = self::DIR_ASCENDING)
    {
        $direction = array_key_exists(strtoupper($direction), $this->shortDirections)
            ? $this->shortDirections[strtoupper($direction)]
            : $direction;

        if (!in_array($direction, $this->allowedDirections)) {
            throw new \InvalidArgumentException(sprintf(
                'Direction %s is not a valid direction',
                $direction
            ));
        }

        if ($this->queryHas('startAt') || $this->queryHas('endAt')) {
            throw new \InvalidArgumentException(
                'Cannot specify an orderBy constraint after calling any of ' .
                '`startAt()`, `startAfter()`, `endBefore()` or `endAt`().'
            );
        }

        if (!($fieldPath instanceof FieldPath)) {
            $fieldPath = FieldPath::fromString($fieldPath);
        }

        return $this->newQuery([
            'orderBy' => [
                [
                    'field' => [
                        'fieldPath' => $fieldPath->pathString()
                    ],
                    'direction' => $direction
                ]
            ]
        ]);
    }
public function orderBy($fieldPath, $direction = self::DIR_ASCENDING)
    {
        $direction = array_key_exists(strtoupper($direction), $this->shortDirections)
            ? $this->shortDirections[strtoupper($direction)]
            : $direction;

        if (!in_array($direction, $this->allowedDirections)) {
            throw new \InvalidArgumentException(sprintf(
                'Direction %s is not a valid direction',
                $direction
            ));
        }

        if ($this->queryHas('startAt') || $this->queryHas('endAt')) {
            throw new \InvalidArgumentException(
                'Cannot specify an orderBy constraint after calling any of ' .
                '`startAt()`, `startAfter()`, `endBefore()` or `endAt`().'
            );
        }

        if (!($fieldPath instanceof FieldPath)) {
            $fieldPath = FieldPath::fromString($fieldPath);
        }

        return $this->newQuery([
            'orderBy' => [
                [
                    'field' => [
                        'fieldPath' => $fieldPath->pathString()
                    ],
                    'direction' => $direction
                ]
            ]
        ]);
    }
public function orderBy ( $ fieldPath , $ direction = self :: DIR_ASCENDING ) { $ direction = array_key_exists ( strtoupper ( $ direction ) , $ this -> shortDirections ) ? $ this -> shortDirections [ strtoupper ( $ direction ) ] : $ direction ; if ( ! in_array ( $ direction , $ this -> allowedDirections ) ) { throw new \ InvalidArgumentException ( sprintf ( 'Direction %s is not a valid direction' , $ direction ) ) ; } if ( $ this -> queryHas ( 'startAt' ) || $ this -> queryHas ( 'endAt' ) ) { throw new \ InvalidArgumentException ( 'Cannot specify an orderBy constraint after calling any of ' . '`startAt()`, `startAfter()`, `endBefore()` or `endAt`().' ) ; } if ( ! ( $ fieldPath instanceof FieldPath ) ) { $ fieldPath = FieldPath :: fromString ( $ fieldPath ) ; } return $ this -> newQuery ( [ 'orderBy' => [ [ 'field' => [ 'fieldPath' => $ fieldPath -> pathString ( ) ] , 'direction' => $ direction ] ] ] ) ; }
Add an ORDER BY clause to the Query.

Example:
```
$query = $query->orderBy('firstName', 'DESC');
```

@param string|FieldPath $fieldPath The field to order by.
@param string $direction The direction to order in. **Defaults to** `ASC`.
@return Query A new instance of Query with the given changes applied.
@throws \InvalidArgumentException If an invalid direction is given.
@throws \InvalidArgumentException If orderBy is called after `startAt()`,
`startAfter()`, `endBefore()` or `endAt`().
Add an ORDER BY clause to the Query .