phpsortingksortasort

Writing my own asort and ksort function in php


Could someone point me in the right direction as to how i would go about writing my own asort and ksort function please?

I'm still new to php...thank you!

Edit: This is what i would like to sort:

$employeeAges = array();
$employeeAges["Lisa"] = "28";
$employeeAges["Jack"] = "16";
$employeeAges["Ryan"] = "35";
$employeeAges["Rachel"] = "46";
$employeeAges["Grace"] = "34";

foreach( $employeeAges as $name => $age){
    echo "Name: $name, Age: $age <br />";
}

I'd want my_sort($employeeAges); to do exactly the same as asort but i have to write the function myself

Edit: This is homework...otherwise i'd simply use the functions, just need a push in the right direction, i dont expect the code. Thanks


Solution

  • I don't know how your homework is specifically worded but if its that you can't use asort or ksort then you can do things like ...

    function my_sort(&$my_array){
        arsort($my_array); // not using asort were using arsort
        $my_array = array_reverse($my_array,true);
    }