Create the 'order' find condition part for a sorted field.

@param string $field The field name to sort
@param string $dir The sort direction (asc, desc)
@param array $options The current find options where the sorting should be added.
@return mixed
171713
protected function _createSortFieldOption($field, $dir, $options)
    {
        $sortField = $this->sortFields[$field];
        if (isset($sortField['custom'])) {
            if (!is_array($sortField['custom'])) {
                $sortField['custom'] = [$sortField['custom']];
            }
            foreach ($sortField['custom'] as $sortEntry) {
                $options['order'][] = preg_replace('/:dir/', $dir, $sortEntry);
            }
        } else {
            $options['order'][] = $sortField['modelField'] . ' ' . $dir;
        }

        return $options;
    }
209168
public function orderBy($field, $sort)
    {
        $this->orderBy = $field;
        $this->sortBy  = $sort;
        return $this;
    }
377592
protected function orderByDirection($field, $direction, $prefixWithTableAlias = true)
    {
        $aliasInsert = $prefixWithTableAlias ? '?f:alias:.' : '';

        if ($this->orderSql == '') {
            $this->orderSql = " ORDER BY {$aliasInsert}?f {$direction}";
        } else {
            $this->orderSql .= ", {$aliasInsert}?f {$direction}";
        }
        $this->orderArguments[] = $field;
    }
1358589
public function pushEachSort($field, array $sort)
    {
        // add modifiers
        if (empty($sort)) {
            throw new Exception('Sort condition is empty');
        }
        
        if (!isset($this->operators['$push'][$field]['$each'])) {
            throw new Exception('Field ' . $field . ' must be pushed with $each modifier');
        }
        
        $this->operators['$push'][$field]['$sort'] = $sort;
        
        return $this;
    }
322005
public function last(array $options = []) {
        if ($order = $this->getOrderBy()) {
            $this->_orderBy = [];

            foreach ($order as $field => $dir) {
                if ($dir === 'asc') {
                    $dir = 'desc';
                } else if ($dir === 'desc') {
                    $dir = 'asc';
                }

                if (is_numeric($field)) {
                    $this->orderBy($dir);
                } else {
                    $this->orderBy($field, $dir);
                }
            }
        } else {
            $this->orderBy($this->getRepository()->getPrimaryKey(), 'desc');
        }

        return $this->limit(1)->find('first', $options);
    }
696847
def orderBy(self, field, direct="desc"):
        
        if direct == "desc":
            self._query = self._query.order_by(r.desc(field))
        else:
            self._query = self._query.order_by(r.asc(field))

        return self
456469
public function orderBy($fields, string $direction = 'ASC'): IQueryBuilderHandler
    {
        if (\is_array($fields) === false) {
            $fields = [$fields];
        }

        foreach ((array)$fields as $key => $value) {
            $field = $key;
            $type = $value;

            if (\is_int($key) === true) {
                $field = $value;
                $type = $direction;
            }

            if (($field instanceof Raw) === false) {
                $field = $this->addTablePrefix($field);
            }

            $this->statements['orderBys'][] = compact('field', 'type');
        }

        return $this;
    }
1016576
public function sortBy($field, $options = SORT_REGULAR, $descending = false)
    {
        $results = [];

        foreach ($this->divisions as $key => $value) {
            $meta = $value->toArray();
            $results[$key] = $meta[$field];
        }

        $descending ? arsort($results, $options)
                    : asort($results, $options);
        
        foreach (array_keys($results) as $key) {
            $results[$key] = $this->divisions[$key];
        }

        return new static($this->manager, $results);
    }
864738
public function generateSortingLink($field, $sortByDir)
    {
        $params = array();
        $params['sortByField'] = $field;
        $params['sortByDir'] = $sortByDir;

        foreach ($params as $key => $value) {
            $params[$key] = $this->cObj->wrap($value, $this->pObj->prefixId . '[' . $key . ']=|');
        }

        $conf = array();
        $conf['parameter'] = $GLOBALS['TSFE']->id;
        $conf['addQueryString'] = '1';
        $conf['addQueryString.']['exclude'] = 'id,tx_kesearch_pi1[multi],cHash';
        $conf['additionalParams'] = '&' . implode('&', $params);

        return $this->cObj->typoLink(
            $this->pObj->pi_getLL('orderlink_' . $field, $field),
            $conf
        );
    }
539768
public function sort(array $fields)
	{
		$sortFields = [];
		foreach ($fields as $field => $value) {
			// Simplest form, just a field name.
			if (is_numeric($field) && !is_array($value)) {
				$sortFields[] = $value;
			} // Field with direction and/or other options.
			else {
				$sortFields[] = [$field => $value];
			}
		}

		$this->set('sort', $sortFields);

		return $this;
	}
206771
public function byNestedField(
        string $field,
        string $order,
        string $mode = self::MODE_AVG
    ): SortBy {
        $this->sortsBy[] = [
            'type' => self::TYPE_NESTED,
            'mode' => $mode,
            'field' => "indexed_metadata.$field",
            'order' => $order,
        ];

        return $this;
    }
973724
final public function setListOrderBy($fields, $direction = "ASC")
    {

        $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' ' . $direction : ' ASC';
        $orderby = NULL;
        //Clean up the order by field list
        if (!empty($fields) && !is_array($fields)) {
            $temp = array();
            foreach (explode(',', $fields) as $part) {
                $part = trim($part);
                $temp[] = $part;
            }

            $orderby = implode(', ', $temp);
        } else if (is_array($fields)) {
            $temp = array();
            foreach ($fields as $field) {
                $part = trim($field);
                $temp[] = $part;
            }
            $orderby = implode(', ', $temp);
        }


        if (!empty($orderby)) {
            $this->listOrderByStatement = "\nORDER BY " . $orderby . $direction;
        }
        //Return this object
        return $this;
    }
104107
public function sortByField($field, $collection)
    {
        if ($collection->isEmpty()) {
            return 'N/A';
        }

        return $collection->groupBy($field)->sortByDesc(function ($logs) {
            return count($logs);
        })->first()->pluck($field)->first();
    }
1164194
public static function sortDocumentsByField($documents, $field, $order = 'ASC')
    {
        self::$orderByField = $field;
        self::$order = strtoupper($order) === 'ASC' ? 'ASC' : 'DESC';
        usort($documents, '\CloudControl\Cms\util\DocumentSorter::fieldCompare');
        if ($order === 'DESC') {
            return array_reverse($documents);
        }
        return $documents;
    }
765418
public function order($field, $order = '')
    {
        if (is_array($field)) {
            $this->options['sort'] = $field;
        } else {
            $this->options['sort'][$field] = 'asc' == strtolower($order) ? 1 : -1;
        }
        return $this;
    }
1214027
public function byFieldValue(
        string $field,
        string $order
    ): SortBy {
        $this->sortsBy[] = [
            'type' => self::TYPE_FIELD,
            'field' => Item::getPathByField($field),
            'order' => $order,
        ];

        return $this;
    }
1286539
public function sortLink($name, $field, $options = [])
    {
        $url = $this->_getSortUrl($field);

        $iClass = 'icon-sortable';
        if (isset($this->activeSort[$field])) {
            $iClass .= '-' . $this->activeSort[$field];
        }

        if (isset($options['icon-theme'])) {
            $iClass .= ' ' . $options['icon-theme'];
            unset($options['icon-theme']);
        }

        $name = '<span>' . $name . '</span><i class="' . $iClass . '"></i>';
        $options['escape'] = false;

        return $this->Html->link($name, $url, $options);
    }