tags:

views:

160

answers:

1

Quick question: How would I switch the sort order between ascending/descending in the following function? All it does is order a multidimensional array by a chosen field, and then by title.

$sortby = 'date';
$orderby = 'asc';
function sort($a, $b)
    {
        $retval = strnatcmp($a[$sortby], $b[$sortby]);
        if(!$retval) return strnatcmp($a['title'], $b['title']);
        return $retval;
    }


uasort($jobs, 'sort');

I'll be very grateful for any help. :)

A: 

There is no reverse option - you'd have to create a new sort function that returns the negative of your sort function.

Simple but inefficient:

function rsort($a, $b)
{
    return -1 * sort($a, $b);
}
Greg
Will that work for alphabetical sorting though?
Throlkim
how aboutfunction rsort($a, $b) { return sort($b, $a); }
grantwparks