Helper.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Helper class for sorting arrays on arbitrary criteria for usort/uasort.
  4. *
  5. * Copyright 2003-2013 Horde LLC (http://www.horde.org/)
  6. *
  7. * See the enclosed file COPYING for license information (LGPL). If you
  8. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  9. *
  10. * @author Marko Djukic <marko@oblo.com>
  11. * @author Jan Schneider <jan@horde.org>
  12. * @author Michael Slusarz <slusarz@horde.org>
  13. * @category Horde
  14. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  15. * @package Util
  16. */
  17. class Horde_Array_Sort_Helper
  18. {
  19. /**
  20. * The array key to sort by.
  21. *
  22. * @var string
  23. */
  24. public $key;
  25. /**
  26. * Compare two associative arrays by the array key defined in self::$key.
  27. *
  28. * @param array $a
  29. * @param array $b
  30. */
  31. public function compare($a, $b)
  32. {
  33. return strcoll(Horde_String::lower($a[$this->key], true, 'UTF-8'), Horde_String::lower($b[$this->key], true, 'UTF-8'));
  34. }
  35. /**
  36. * Compare, in reverse order, two associative arrays by the array key
  37. * defined in self::$key.
  38. *
  39. * @param scalar $a TODO
  40. * @param scalar $b TODO
  41. *
  42. * @return TODO
  43. */
  44. public function reverseCompare($a, $b)
  45. {
  46. return strcoll(Horde_String::lower($b[$this->key], true, 'UTF-8'), Horde_String::lower($a[$this->key], true, 'UTF-8'));
  47. }
  48. /**
  49. * Compare array keys case insensitively for uksort.
  50. *
  51. * @param scalar $a TODO
  52. * @param scalar $b TODO
  53. *
  54. * @return TODO
  55. */
  56. public function compareKeys($a, $b)
  57. {
  58. return strcoll(Horde_String::lower($a, true, 'UTF-8'), Horde_String::lower($b, true, 'UTF-8'));
  59. }
  60. /**
  61. * Compare, in reverse order, array keys case insensitively for uksort.
  62. *
  63. * @param scalar $a TODO
  64. * @param scalar $b TODO
  65. *
  66. * @return TODO
  67. */
  68. public function reverseCompareKeys($a, $b)
  69. {
  70. return strcoll(Horde_String::lower($b, true, 'UTF-8'), Horde_String::lower($a, true, 'UTF-8'));
  71. }
  72. }