Whups::_sort PHP Method

_sort() protected static method

Used as callback to usort().
protected static _sort ( array $a, array $b, string $sortby = null, string $sortdir = null ) : integer
$a array The first ticket to compare.
$b array The second ticket to compare.
$sortby string The field to sort by. If null, uses the field from self::sortBy().
$sortdir string The direction to sort. If null, uses the value from self::sortDir().
return integer
    protected static function _sort($a, $b, $sortby = null, $sortdir = null)
    {
        if (is_null($sortby)) {
            $sortby = self::$_sortBy;
        }
        if (is_null($sortdir)) {
            $sortdir = self::$_sortDir;
        }
        if (is_array($sortby)) {
            if (!isset($a[$sortby[0]])) {
                $a[$sortby[0]] = null;
            }
            if (!isset($b[$sortby[0]])) {
                $b[$sortby[0]] = null;
            }
            if (!count($sortby)) {
                return 0;
            }
            if ($a['sort_by'][$sortby[0]] > $b['sort_by'][$sortby[0]]) {
                return $sortdir[0] ? -1 : 1;
            }
            if ($a['sort_by'][$sortby[0]] === $b['sort_by'][$sortby[0]]) {
                array_shift($sortby);
                array_shift($sortdir);
                return self::_sort($a, $b, $sortby, $sortdir);
            }
            return $sortdir[0] ? 1 : -1;
        }
        $a_val = isset($a['sort_by'][$sortby]) ? $a['sort_by'][$sortby] : null;
        $b_val = isset($b['sort_by'][$sortby]) ? $b['sort_by'][$sortby] : null;
        // Take care of the simplest case first
        if ($a_val === $b_val) {
            return 0;
        }
        if ((is_numeric($a_val) || is_null($a_val)) && (is_numeric($b_val) || is_null($b_val))) {
            // Numeric comparison
            return (int) ($sortdir ? $b_val > $a_val : $a_val > $b_val);
        }
        // Some special case sorting
        if (is_array($a_val) || is_array($b_val)) {
            $a_val = implode('', $a_val);
            $b_val = implode('', $b_val);
        }
        // String comparison
        return $sortdir ? strcoll($b_val, $a_val) : strcoll($a_val, $b_val);
    }