Sorting PHP array without ksort -
i trying manually sort php array without making use of ksort.
this how code looks @ moment:
function my_ksort(&$arg){ foreach($arg $key1 => $value1){ foreach($arg $key2 => $value2){ if($key1 > $key2){ $aux = $value2; $arg[$key2] = $value1; $arg[$key1] = $aux; } } } }
it doesn't sort, can't figure out how make sort.
you try this:
function my_ksort(&$arg) { $keys=array_keys($arg); sort($keys); foreach($keys $key) { $val=$arg[$key]; unset($arg[$key]); $arg[$key]=$val; } }
i'm sorting keys separately , deleting elements one-by-one , appending them end, in ascending order.
i'm using sorting function (sort()
), if want eliminate available sorting functions emulation, sort()
easier emulate. in fact, @crypticous's algorithm that!
Comments
Post a Comment