array_order.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. class array_order {
  3. var $sort_fields;
  4. var $backwards = false;
  5. var $numeric = false;
  6. function sort() {
  7. $args = func_get_args();
  8. $array = $args[0];
  9. if (!$array) return array();
  10. $this->sort_fields = array_slice($args, 1);
  11. if (!$this->sort_fields) return $array();
  12. if ($this->numeric) {
  13. usort($array, array($this, 'numericCompare'));
  14. } else {
  15. usort($array, array($this, 'stringCompare'));
  16. }
  17. return $array;
  18. }
  19. function numericCompare($a, $b) {
  20. foreach($this->sort_fields as $sort_field) {
  21. if ($a[$sort_field] == $b[$sort_field]) {
  22. continue;
  23. }
  24. return ($a[$sort_field] < $b[$sort_field]) ? ($this->backwards ? 1 : -1) : ($this->backwards ? -1 : 1);
  25. }
  26. return 0;
  27. }
  28. function stringCompare($a, $b) {
  29. foreach($this->sort_fields as $sort_field) {
  30. $cmp_result = strcasecmp($a[$sort_field], $b[$sort_field]);
  31. if ($cmp_result == 0) continue;
  32. return ($this->backwards ? -$cmp_result : $cmp_result);
  33. }
  34. return 0;
  35. }
  36. }
  37. //$order = new array_order();
  38. //$registrations = $order->sort($registrations, 'domain', 'user');
  39. ?>