php - How to sort an SplFixedArray? -
is there way perform sorting on integers or strings in instance of splfixedarray
class? converting php's array
, sorting, , converting being option?
firstly, congratulations on finding , using splfixedarrays! think they're highly under-utilised feature in vanilla php ...
as you've appreciated, performance unrivalled (compared usual php arrays) - come @ trade-offs, including lack of php functions sort them (which shame)!
implementing own bubble-sort relatively easy , efficient solution. iterate through, looking @ each consecutive pairs of elements, putting highest on right. rinse , repeat until array sorted:
<?php $arr = new splfixedarray(10); $arr[0] = 2345; $arr[1] = 314; $arr[2] = 3666; $arr[3] = 93; $arr[4] = 7542; $arr[5] = 4253; $arr[6] = 2343; $arr[7] = 32; $arr[8] = 6324; $arr[9] = 1; $moved = 0; while ($moved < sizeof($arr) - 1) { $i = 0; while ($i < sizeof($arr) - 1 - $moved) { if ($arr[$i] > $arr[$i + 1]) { $tmp = $arr[$i + 1]; $arr[$i + 1] = $arr[$i]; $arr[$i] = $tmp; } $i++; var_dump ($arr); } $moved++; }
it's not fast, it's not efficient. might consider quicksort - there's documented examples online including 1 @ wikibooks.org (will need modification of work splfixedarrays).
seriously, beyond getting question answered, feel forcing ask why things splfixedarray exist , forcing understand goes on behind "quick call array_sort()
" (and why takes long time run) make difference between programmers , programmers. applaud question!
Comments
Post a Comment