SortArray.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Drupal\Component\Utility;
  3. /**
  4. * Provides generic array sorting helper methods.
  5. *
  6. * @ingroup utility
  7. */
  8. class SortArray {
  9. /**
  10. * Sorts a structured array by the 'weight' element.
  11. *
  12. * Note that the sorting is by the 'weight' array element, not by the render
  13. * element property '#weight'.
  14. *
  15. * Callback for uasort().
  16. *
  17. * @param array $a
  18. * First item for comparison. The compared items should be associative
  19. * arrays that optionally include a 'weight' element. For items without a
  20. * 'weight' element, a default value of 0 will be used.
  21. * @param array $b
  22. * Second item for comparison.
  23. *
  24. * @return int
  25. * The comparison result for uasort().
  26. */
  27. public static function sortByWeightElement(array $a, array $b) {
  28. return static::sortByKeyInt($a, $b, 'weight');
  29. }
  30. /**
  31. * Sorts a structured array by '#weight' property.
  32. *
  33. * Callback for uasort().
  34. *
  35. * @param array $a
  36. * First item for comparison. The compared items should be associative
  37. * arrays that optionally include a '#weight' key.
  38. * @param array $b
  39. * Second item for comparison.
  40. *
  41. * @return int
  42. * The comparison result for uasort().
  43. */
  44. public static function sortByWeightProperty($a, $b) {
  45. return static::sortByKeyInt($a, $b, '#weight');
  46. }
  47. /**
  48. * Sorts a structured array by 'title' key (no # prefix).
  49. *
  50. * Callback for uasort().
  51. *
  52. * @param array $a
  53. * First item for comparison. The compared items should be associative arrays
  54. * that optionally include a 'title' key.
  55. * @param array $b
  56. * Second item for comparison.
  57. *
  58. * @return int
  59. * The comparison result for uasort().
  60. */
  61. public static function sortByTitleElement($a, $b) {
  62. return static::sortByKeyString($a, $b, 'title');
  63. }
  64. /**
  65. * Sorts a structured array by '#title' property.
  66. *
  67. * Callback for uasort().
  68. *
  69. * @param array $a
  70. * First item for comparison. The compared items should be associative arrays
  71. * that optionally include a '#title' key.
  72. * @param array $b
  73. * Second item for comparison.
  74. *
  75. * @return int
  76. * The comparison result for uasort().
  77. */
  78. public static function sortByTitleProperty($a, $b) {
  79. return static::sortByKeyString($a, $b, '#title');
  80. }
  81. /**
  82. * Sorts a string array item by an arbitrary key.
  83. *
  84. * @param array $a
  85. * First item for comparison.
  86. * @param array $b
  87. * Second item for comparison.
  88. * @param string $key
  89. * The key to use in the comparison.
  90. *
  91. * @return int
  92. * The comparison result for uasort().
  93. */
  94. public static function sortByKeyString($a, $b, $key) {
  95. $a_title = (is_array($a) && isset($a[$key])) ? $a[$key] : '';
  96. $b_title = (is_array($b) && isset($b[$key])) ? $b[$key] : '';
  97. return strnatcasecmp($a_title, $b_title);
  98. }
  99. /**
  100. * Sorts an integer array item by an arbitrary key.
  101. *
  102. * @param array $a
  103. * First item for comparison.
  104. * @param array $b
  105. * Second item for comparison.
  106. * @param string $key
  107. * The key to use in the comparison.
  108. *
  109. * @return int
  110. * The comparison result for uasort().
  111. */
  112. public static function sortByKeyInt($a, $b, $key) {
  113. $a_weight = (is_array($a) && isset($a[$key])) ? $a[$key] : 0;
  114. $b_weight = (is_array($b) && isset($b[$key])) ? $b[$key] : 0;
  115. if ($a_weight == $b_weight) {
  116. return 0;
  117. }
  118. return ($a_weight < $b_weight) ? -1 : 1;
  119. }
  120. }