src/ORM/DataList.php
public function sort()
    {
        $count = func_num_args();

        if ($count == 0) {
            return $this;
        }

        if ($count > 2) {
            throw new InvalidArgumentException('This method takes zero, one or two arguments');
        }

        if ($count == 2) {
            $col = null;
            $dir = null;
            list($col, $dir) = func_get_args();

            // Validate direction
            if (!in_array(strtolower($dir), ['desc', 'asc'])) {
                user_error('Second argument to sort must be either ASC or DESC');
            }

            $sort = [$col => $dir];
        } else {
            $sort = func_get_arg(0);
        }

        return $this->alterDataQuery(function (DataQuery $query, DataList $list) use ($sort) {

            if (is_string($sort) && $sort) {
                if (false !== stripos($sort, ' asc') || false !== stripos($sort, ' desc')) {
                    $query->sort($sort);
                } else {
                    $list->applyRelation($sort, $column, true);
                    $query->sort($column, 'ASC');
                }
            } elseif (is_array($sort)) {
                // sort(array('Name'=>'desc'));
                $query->sort(null, null); // wipe the sort

                foreach ($sort as $column => $direction) {
                    // Convert column expressions to SQL fragment, while still allowing the passing of raw SQL
                    // fragments.
                    $list->applyRelation($column, $relationColumn, true);
                    $query->sort($relationColumn, $direction, false);
                }
            }
        });
    }
public function sort()
    {
        $count = func_num_args();

        if ($count == 0) {
            return $this;
        }

        if ($count > 2) {
            throw new InvalidArgumentException('This method takes zero, one or two arguments');
        }

        if ($count == 2) {
            $col = null;
            $dir = null;
            list($col, $dir) = func_get_args();

            // Validate direction
            if (!in_array(strtolower($dir), ['desc', 'asc'])) {
                user_error('Second argument to sort must be either ASC or DESC');
            }

            $sort = [$col => $dir];
        } else {
            $sort = func_get_arg(0);
        }

        return $this->alterDataQuery(function (DataQuery $query, DataList $list) use ($sort) {

            if (is_string($sort) && $sort) {
                if (false !== stripos($sort, ' asc') || false !== stripos($sort, ' desc')) {
                    $query->sort($sort);
                } else {
                    $list->applyRelation($sort, $column, true);
                    $query->sort($column, 'ASC');
                }
            } elseif (is_array($sort)) {
                // sort(array('Name'=>'desc'));
                $query->sort(null, null); // wipe the sort

                foreach ($sort as $column => $direction) {
                    // Convert column expressions to SQL fragment, while still allowing the passing of raw SQL
                    // fragments.
                    $list->applyRelation($column, $relationColumn, true);
                    $query->sort($relationColumn, $direction, false);
                }
            }
        });
    }
public function sort ( ) { $ count = func_num_args ( ) ; if ( $ count == 0 ) { return $ this ; } if ( $ count > 2 ) { throw new InvalidArgumentException ( 'This method takes zero, one or two arguments' ) ; } if ( $ count == 2 ) { $ col = null ; $ dir = null ; list ( $ col , $ dir ) = func_get_args ( ) ; // Validate direction if ( ! in_array ( strtolower ( $ dir ) , [ 'desc' , 'asc' ] ) ) { user_error ( 'Second argument to sort must be either ASC or DESC' ) ; } $ sort = [ $ col => $ dir ] ; } else { $ sort = func_get_arg ( 0 ) ; } return $ this -> alterDataQuery ( function ( DataQuery $ query , DataList $ list ) use ( $ sort ) { if ( is_string ( $ sort ) && $ sort ) { if ( false !== stripos ( $ sort , ' asc' ) || false !== stripos ( $ sort , ' desc' ) ) { $ query -> sort ( $ sort ) ; } else { $ list -> applyRelation ( $ sort , $ column , true ) ; $ query -> sort ( $ column , 'ASC' ) ; } } elseif ( is_array ( $ sort ) ) { // sort(array('Name'=>'desc')); $ query -> sort ( null , null ) ; // wipe the sort foreach ( $ sort as $ column => $ direction ) { // Convert column expressions to SQL fragment, while still allowing the passing of raw SQL // fragments. $ list -> applyRelation ( $ column , $ relationColumn , true ) ; $ query -> sort ( $ relationColumn , $ direction , false ) ; } } } ) ; }
Return a new DataList instance as a copy of this data list with the sort
order set.

@see SS_List::sort()
@see SQLSelect::orderby
@example $list = $list->sort('Name'); // default ASC sorting
@example $list = $list->sort('Name DESC'); // DESC sorting
@example $list = $list->sort('Name', 'ASC');
@example $list = $list->sort(array('Name'=>'ASC', 'Age'=>'DESC'));

@param String|array Escaped SQL statement. If passed as array, all keys and values are assumed to be escaped.
@return static
Return a new DataList instance as a copy of this data list with the sort order set .