Posted By

luman on 08/03/06


Tagged

sort array


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

meth
tonic


Order mutidimensional Array


 / Published in: PHP
 

  1. function array_qsort (&$array, $column=0, $order=SORT_ASC, $first=0, $last= -2)
  2. {
  3. // $array - the array to be sorted
  4. // $column - index (column) on which to sort
  5. // can be a string if using an associative array
  6. // $order - SORT_ASC (default) for ascending or SORT_DESC for descending
  7. // $first - start index (row) for partial array sort
  8. // $last - stop index (row) for partial array sort
  9. // $keys - array of key values for hash array sort
  10.  
  11. $keys = array_keys($array);
  12. if($last == -2) $last = count($array) - 1;
  13. if($last > $first) {
  14. $alpha = $first;
  15. $omega = $last;
  16. $key_alpha = $keys[$alpha];
  17. $key_omega = $keys[$omega];
  18. $guess = $array[$key_alpha][$column];
  19. while($omega >= $alpha) {
  20. if($order == SORT_ASC) {
  21. while($array[$key_alpha][$column] < $guess) {$alpha++; $key_alpha = $keys[$alpha]; }
  22. while($array[$key_omega][$column] > $guess) {$omega--; $key_omega = $keys[$omega]; }
  23. } else {
  24. while($array[$key_alpha][$column] > $guess) {$alpha++; $key_alpha = $keys[$alpha]; }
  25. while($array[$key_omega][$column] < $guess) {$omega--; $key_omega = $keys[$omega]; }
  26. }
  27. if($alpha > $omega) break;
  28. $temporary = $array[$key_alpha];
  29. $array[$key_alpha] = $array[$key_omega]; $alpha++;
  30. $key_alpha = $keys[$alpha];
  31. $array[$key_omega] = $temporary; $omega--;
  32. $key_omega = $keys[$omega];
  33. }
  34. array_qsort ($array, $column, $order, $first, $omega);
  35. array_qsort ($array, $column, $order, $alpha, $last);
  36. }
  37. }

Report this snippet  

You need to login to post a comment.