Logic for sorting the underlying database.

@param string $key
@param string $order

@return void
196997
public function sort($key, $order)
    {
        uasort($this->db, function ($a, $b) use ($key, $order) {
            if (is_object($a) && is_object($b)) {
                return $order == 'asc'
                    ? strnatcasecmp($a->$key, $b->$key)
                    : -strnatcasecmp($a->$key, $b->$key);
            } elseif (is_array($a) && is_array($b)) {
                return $order == 'asc'
                    ? strnatcasecmp($a[$key], $b[$key])
                    : -strnatcasecmp($a[$key], $b[$key]);
            }

            throw new InvalidArgumentException('Can only sort by keys or properties of non-scalar types');
        });
    }
99925
public function newOrder($order)
    {
        $sorter = new Sorter();
        $sorter->target = $this;
        $sorter->order = $order;
        $sorter->query = (new Query())
                            ->from(static::tableName())
                            ->where(['and',
                                ['!=', 'id', $this->id],
                                ['category_id' => $this->category_id]
                            ])
                            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC])
                            ->indexBy('id');
        return $sorter->run();
    }
1282679
public function addOrderBy(array $orderBy): void
    {
        if (empty($orderBy)) {
            return;
        }
        $orderByList = [];
        foreach ($orderBy as $clause) {
            $clause['key'] = $this->quoteName($clause['key']);
            array_push($orderByList, $clause['key'] . ' ' . $clause['direction']);
        }
        $pattern = ' ORDER BY %s';
        $this->statement .= sprintf($pattern, implode(', ', $orderByList));
        $this->statement .= PHP_EOL;
    }
224664
public function create(Order &$order)
    {
        $data = $order->exportData();
        $endpoint = '/admin/orders.json';
        $response = $this->request(
            $endpoint, 'POST', array(
            'order' => $data
            )
        );
        $order->setData($response['order']);
    }
85491
public function setOrder($column, $order = self::ORDER_ASC): void
    {
        $this->orders = [];
        $this->addOrder($column, $order);
    }
1152872
public function sort($key = null, $rule = "", $save_key = true)
    {
        $this->sortArray($key, $rule, 'asc', $save_key);
        return $this;
    }
750634
public function addOrderBy($sort, $order = null)
    {
        return $this->add('orderBy', $sort . ' ' . (! $order ? 'ASC' : $order), true);
    }
252541
public static function sortByKey($_array, $key) {
        $array = $_array;
        $result = usort($array, function($a, $b) use ($key) {
            return strcmp(call_user_func(array($a, $key)),
                          call_user_func(array($b, $key)));
        });
        if (!$result)
            throw new ArrayException("sorting by \"$key\" failed");
        return $array;
    }
2310
public function byFunction(
        string $function,
        string $order
    ): SortBy {
        $this->sortsBy[] = [
            'type' => self::TYPE_FUNCTION,
            'function' => $function,
            'order' => $order,
        ];

        return $this;
    }
1182805
public function sortingPriority($key)
    {
        $this->bootCanSortMethodsOnce();

        if (!isset($this->sorting[$key])) {
            return 0;
        }

        return array_search($key, array_keys($this->sorting)) + 1;
    }
1250855
public function order($order)
    {
        $order = $this->prepareNames(func_get_args(),false);
        $this->order = $order;
        return $this;
    }
666193
public function orderBy($key)
    {
        if ($this instanceof Entity) {
            $key = $this->extendedKey($key);
        }

        $this->getQuery()->orderBy($key);

        return $this;
    }
672239
private function addSearchOrderColumn($key, $columnTableName, $data)
    {
        $column = $this->columns[$key];

        true === $column->getOrderable() ? $this->orderColumns[] = $columnTableName.'.'.$data : $this->orderColumns[] = null;
        true === $column->getSearchable() ? $this->searchColumns[] = $columnTableName.'.'.$data : $this->searchColumns[] = null;
    }
224227
public function isSortableBy($key)
    {
        // Sorting is not allowed on Model Admin
        if (!$this->isSortable()) {
            return false;
        }

        // Key results are mutated, so sorting is not available
        if ($this->model()->hasGetMutator($key) || $this->hasGetMutator($key)) {
            return false;
        }

        // Key is a relation, so sorting is not available 
        if (strpos($key, '.') !== false) {
            return false;
        }

        return true;
    }
661053
public function orderBy($key, $mode = 'ASC', $options = SORT_REGULAR)
    {
        return $this->sortBy($key, $options, ($mode === 'DESC' ? true : false));
    }
597381
private function sortArray($key = null, $sortRule = "", $order = "asc", $save_key = true)
    {
        $array = $this->get($key);

        if (!is_array($array)) {
            return false;
        }

        /**
         * $array = [
         *              ["book"=>10,"version"=>10],
         *              ["book"=>19,"version"=>30],
         *              ["book"=>10,"version"=>30],
         *              ["book"=>19,"version"=>10],
         *              ["book"=>10,"version"=>20],
         *              ["book"=>19,"version"=>20]
         *      ];
         */
        if (is_array($sortRule)) {
            /**
             * $sortRule = ['book'=>"asc",'version'=>"asc"];
             */
            usort($array, function ($a, $b) use ($sortRule) {
                foreach ($sortRule as $sortKey => $order) {
                    if ($a[$sortKey] == $b[$sortKey]) {
                        continue;
                    }
                    return (($order != 'asc') ? -1 : 1) * (($a[$sortKey] < $b[$sortKey]) ? -1 : 1);
                }
                return 0;
            });
        } else if (is_string($sortRule)) {
            if (!empty($sortRule)) {
                /**
                 * $sortRule = "book";
                 * $order = "asc";
                 */
                usort($array, function ($a, $b) use ($sortRule, $order) {
                    if ($a[$sortRule] == $b[$sortRule]) {
                        return 0;
                    }
                    return (($order != 'asc') ? -1 : 1) * (($a[$sortRule] < $b[$sortRule]) ? -1 : 1);
                });
            } else {
                if ($save_key) {
                    $order == 'asc' ? asort($array) : arsort($array);
                } else {
                    usort($array, function ($a, $b) use ($order) {
                        if ($a == $b) {
                            return 0;
                        }
                        return (($order != 'asc') ? -1 : 1) * (($a < $b) ? -1 : 1);
                    });
                }
            }
        }

        is_null($key) ? $this->array = $array : $this->set($key, $array);
        return $this->array;
    }
1344585
public function addSort($field, $order = self::SORT_DECREASE) {
        if ($this->searchParams->sort == null) {
            $this->searchParams->sort = new Sort();
            $this->searchParams->sort->sortFields = array();
        }
        $sortField = new SortField(array('field' => $field, 'order' => $order));
        $this->searchParams->sort->sortFields[] = $sortField;
    }