Question

I have this awful sorting function, which started giving me problems when two were called on the same page. It is highly likely that it would be called many times on the same page.

I wish to be able to call the sorting function and send specific parameters to determine the type of sort. These include the field that is being sorted by ($arr['key']), whether it's a string or integer (strcmp vs x - y) and also the direction to compare (asc vs desc).

How can I do this in a non-crazy, complicating and messy way? I tried lots and lots of searching, but I found lots of things using objects and classes and it confused me and I didn't see how to send more parameters.

Any help, even if this is a duplicate question and you have the answer, would be greatly greatly appreciated!

//awful sorting function
switch($sort) {
case 'photoname_asc':
    function a_compare($a, $b) {
        $t1 = $a['photoname'];
        $t2 = $b['photoname'];
        return strcmp($t1, $t2);
    }
    usort($photos, 'a_compare');
    default;
    break;
case 'photoname_desc':
    function a_compare($a, $b) {
        $t1 = $a['photoname'];
        $t2 = $b['photoname'];
        return strcmp($t2, $t1);
    }
    usort($photos, 'a_compare');
    break;
case 'dateup_asc':
    function a_compare($a, $b) {
        $t1 = $a['id'];
        $t2 = $b['id'];
        return $t1 - $t2;
    }
    usort($photos, 'a_compare');
    break;
case 'dateup_desc':
    function a_compare($a, $b) {
        $t1 = $a['id'];
        $t2 = $b['id'];
        return $t2 - $t1;
    }
    usort($photos, 'a_compare');
    break;
 }

Thanks so very much.

Était-ce utile?

La solution

Here is an example of a function that condenses your switch statement. Warning untested but should do the trick.

function photoStort(array &$photos, $key, $order) {
    if ($order !== 'desc' && $order !== 'asc') {
        return false;
    }

    usort($photos, function($a, $b) use ($key, $order) {
        $t1 = $a[$key];
        $t2 = $b[$key];

        if (is_string($t1) && is_string($t2)) {
            if ($order === 'asc') {
                return strcmp($t1, $t2);
            } else {
                return strcmp($t2, $t1);
            }
        } elseif (is_int($t1) && is_int($t2)) {
            if ($order === 'asc') {
                return $t1 - $t2;
            } else {
                return $t2 - $t1;
            }
        } else {
            trigger_error('Invalid type in photoSort!', E_WARNING);
        }
    });
}
$photos = array(/* Photos */);

//ex 1
photoSort($photos, 'photoname', 'asc');

//ex 2
photoSort($photos, 'id', 'desc');

PHP 5.2 solution

function usortCallback($a, $b, $key, $order) {
    $t1 = $a[$key];
    $t2 = $b[$key];

    if (is_string($t1) && is_string($t2)) {
        if ($order === 'asc') {
            return strcmp($t1, $t2);
        } else {
            return strcmp($t2, $t1);
        }
    } elseif (is_int($t1) && is_int($t2)) {
        if ($order === 'asc') {
            return $t1 - $t2;
        } else {
            return $t2 - $t1;
        }
    } else {
        trigger_error('Invalid type in photoSort!', E_WARNING);
    }
}

function photoSort(array &$photos, $key, $order) {
    if ($order !== 'desc' && $order !== 'asc') {
        return false;
    }

    usort($photos, create_function('$a, $b', 'return usortCallback($a, $b, "' . $key . '", "' . $order . '");'));
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top