处理排序
@param $sort 推荐格式 array('id' => -1, 'name' => 1)
@return array
541907
public static function sort($sort) {

        if ( is_array($sort) ) return $sort;

        $__sort = array();
        if ( is_string($sort) && $sort != '' ) {
            $oarr = explode(',', $sort);
            foreach ($oarr as $value) {
                $value = preg_replace('/\s+/', ' ', $value);    //去除多余的空格
                $value = explode(' ', $value);
                if ( strtoupper($value[1]) == "DESC" ) {
                    $__sort[$value[0]] = -1;
                } else {
                    $__sort[$value[0]] = 1;
                }
            }
        }
        return $__sort;
    }
882550
public function getData($order = self::SORT_DESC)
    {
        return array(
            'items' => array_map(
                function (Item $item) {
                    /*
                     * @var Item $item
                     */
                    return $item->toArray();
                },
                array_values($this->getItems($order))
            ),
        );
    }
613681
public function sort(array $sort = null) {
        if ($sort === null) {
            return $this->sort;
        }

        $sortData = [];

        foreach ($sort as $field) {
            $field = trim($field);
            $dir = 'asc';

            if (empty($field)) {
                throw new RuntimeException('Badly formatted sort', 400);
            }

            if (strpos($field, ':') !== false) {
                list($fieldName, $dir) = explode(':', $field);

                if ($dir !== 'asc' && $dir !== 'desc') {
                    throw new RuntimeException('Invalid sort value: ' . $field, 400);
                }

                $field = $fieldName;
            }

            $sortData[] = [
                'field' => $field,
                'sort' => $dir,
            ];
        }

        $this->sort = $sortData;

        return $this;
    }
642639
public function addSort($sort)
    {
        if (!$this->sorts->contains($sort)) {
            $this->sorts[] = $sort;
        }

        return $this;
    }
778195
public function friends($sort = 'onlineStatus', $offset = 0, $limit = 36) : array
    {
        $result = [];

        $friends = $this->get(sprintf(self::USERS_ENDPOINT . 'friends/profiles2', $this->onlineIdParameter()), [
            'fields' => 'onlineId,accountId,avatarUrls,plus,trophySummary(@default),isOfficiallyVerified,personalDetail(@default,profilePictureUrls),presences(@titleInfo,hasBroadcastData,lastOnlineDate),presences(@titleInfo),friendRelation,consoleAvailability',
            'offset' => $offset,
            'limit' => $limit,
            'profilePictureSizes' => 'm',
            'avatarSizes' => 'm',
            'titleIconSize' => 's',
            'sort' => $sort
        ]);
        
        foreach ($friends->profiles as $friend) {
            $result[] = new self($this->client, $friend);
        }

        return $result;
    }
927878
public function fetchAll($sort = null)
    {
        $select = $this->getFullTree();
        $select = $this->setSortOrder($select, $sort);

        return $this->fetchResult($select);
    }
129111
public function getList($sort = null)
    {
        //Get the states list
        $states = $this->getStates();
        
        //Sorting
        $validSorts = [
            'iso_3166_2',
            'name',
            'country_code',
        ];
        
        if (! is_null($sort) && in_array($sort, $validSorts)) {
            uasort($states, function ($a, $b) use ($sort) {
                if (!isset($a[$sort]) && !isset($b[$sort])) {
                    return 0;
                } elseif (!isset($a[$sort])) {
                    return -1;
                } elseif (!isset($b[$sort])) {
                    return 1;
                } else {
                    return strcasecmp($a[$sort], $b[$sort]);
                }
            });
        }
        
        //Return the states
        return $states;
    }
541003
private function parseSort(array $sort)
    {
        $out = '';
        if (!empty($sort)) {
            $conditions = [];
            foreach ($sort as $field => $direction) {
                $direction = $direction ? 'DESC' : 'ASC';
                $conditions[] = sprintf('%s %s', $field, $direction);
            }
            $out = sprintf(' ORDER BY %s', implode(', ', $conditions));
        }
        return $out;
    }
52359
public function sort($sort)
    {
        if (null !== $sort && !is_array($sort)) {
            throw new \InvalidArgumentException(sprintf('The sort "%s" is not valid.', $sort));
        }

        $this->sort = $sort;

        return $this;
    }
992272
public function buildRequestMysign($para_sort) {
        //把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
        $prestr = HelperFunction::createLinkstring($para_sort);

        $mysign = "";
        switch (strtoupper(trim($this->sign_type))) {
            case "MD5" :
                $mysign = HelperFunction::md5Sign($prestr, $this->key);
                break;
            default :
                $mysign = "";
        }

        return $mysign;
    }
570754
public static function listSortBy($list, $field, $sortBy = 'asc'): array
    {
        if (is_array($list)) {
            $refer = $resultSet = [];
            
            foreach ($list as $i => $data) {
                $refer[$i] = &$data[$field];
            }
            
            switch ($sortBy) {
                // 正向排序
                case 'asc':
                    asort($refer);
                    break;
                // 逆向排序
                case 'desc':
                    arsort($refer);
                    break;
                // 自然排序
                case 'nat':
                    natcasesort($refer);
                    break;
            }
            foreach ($refer as $key => $val) {
                $resultSet[] = &$list[$key];
            }
            
            return $resultSet;
        }
        
        return [];
    }
27081
public function sort($sorts)
    {
        usort($this->rows, 
            function ($a, $b) use ($sorts) {
                $cmp = 0;
                foreach ($sorts as $sort => $direction) {
                    if (is_string($direction)) {
                        $cmp = is_numeric($a[$sort]) && is_numeric($b[$sort]) ? 
                            $a[$sort] - $b[$sort] : strcmp($a[$sort], $b[$sort]);
                        $cmp = $direction === 'asc' ? $cmp : - $cmp;
                    } else if (is_callable($direction)) {
                        $cmp = $direction($a[$sort], $b[$sort]);
                    }
                    if ($cmp !== 0) {
                        break;
                    }
                }
                return $cmp;
            }
        );
        return $this;
    }
1086327
protected function querySort($query, $sort = [])
    {
        $sort = $sort ?: $this->sort;

        foreach ($sort as $attribute => $order) {
            $query->orderBy($attribute, $order);
        }

        return $query;
    }
638378
public function orderByUpdatedAt($sort = SORT_ASC)
    {
        /* @var $this ActiveQuery */
        $model = $this->noInitModel;
        if (!is_string($model->updatedAtAttribute) || empty($model->updatedAtAttribute)) {
            return $this;
        }
        return $this->addOrderBy([$model->updatedAtAttribute => $sort]);
    }
29755
protected function checkOrder($order='') {
         if(is_string($order) && !empty($order)) {
            $orders = explode(',',$order);
            $_order = array();
            foreach ($orders as $order){
                $array  =   explode(' ',trim($order));
                $field  =   $array[0];
                $sort   =   isset($array[1])?$array[1]:'ASC';
                if(array_key_exists($field,$this->mapFields)){
                    // 需要处理映射字段
                    $field  =   $this->mapFields[$field];
                }                
                $_order[] = $field.' '.$sort;
            }
            $order = implode(',',$_order);
         }
        return $order;
    }
313617
public function getSort($sortName): ?OrderBy
    {
        [$name, $sorts] = $this->getArrayAndKeyName($sortName, 'sorts');

        return array_first($sorts, function ($sort) use ($name) {
            return $sort->getName() === $name;
        });
    }
524292
protected function sort($order)
    {
        $ordered = [];

        $matrix = [
            'label'     => 'getLabels',
            'component' => 'getComponents',
            'message'   => 'getMessage',
        ];

        foreach ($order as $identifier) {
            $components = $this->{$matrix[$identifier]}();
            foreach ($components as $component) {
                $index                              = count($ordered);
                $this->indexReverse[$identifier][0] = $index;
                $this->index[$index]                = $identifier;
                $ordered[]                          = $component;
            }
        }

        return $ordered;
    }