class SortArray

Same name in other branches
  1. 9 core/lib/Drupal/Component/Utility/SortArray.php \Drupal\Component\Utility\SortArray
  2. 8.9.x core/lib/Drupal/Component/Utility/SortArray.php \Drupal\Component\Utility\SortArray
  3. 11.x core/lib/Drupal/Component/Utility/SortArray.php \Drupal\Component\Utility\SortArray

Provides generic array sorting helper methods.

Hierarchy

Expanded class hierarchy of SortArray

Related topics

9 files declare their use of SortArray
common.inc in core/includes/common.inc
Common functions that many Drupal modules will need to reference.
ContextualLinks.php in core/modules/contextual/src/Element/ContextualLinks.php
FieldStorageAddForm.php in core/modules/field_ui/src/Form/FieldStorageAddForm.php
Finder.php in core/lib/Drupal/Core/DefaultContent/Finder.php
HelpTopicPluginController.php in core/modules/help/src/Controller/HelpTopicPluginController.php

... See full list

File

core/lib/Drupal/Component/Utility/SortArray.php, line 10

Namespace

Drupal\Component\Utility
View source
class SortArray {
    
    /**
     * Sorts a structured array by the 'weight' element.
     *
     * Note that the sorting is by the 'weight' array element, not by the render
     * element property '#weight'.
     *
     * Callback for uasort().
     *
     * @param array $a
     *   First item for comparison. The compared items should be associative
     *   arrays that optionally include a 'weight' element. For items without a
     *   'weight' element, a default value of 0 will be used.
     * @param array $b
     *   Second item for comparison.
     *
     * @return int
     *   The comparison result for uasort().
     */
    public static function sortByWeightElement(array $a, array $b) {
        return static::sortByKeyInt($a, $b, 'weight');
    }
    
    /**
     * Sorts a structured array by '#weight' property.
     *
     * Callback for uasort().
     *
     * @param array $a
     *   First item for comparison. The compared items should be associative
     *   arrays that optionally include a '#weight' key.
     * @param array $b
     *   Second item for comparison.
     *
     * @return int
     *   The comparison result for uasort().
     */
    public static function sortByWeightProperty($a, $b) {
        return static::sortByKeyInt($a, $b, '#weight');
    }
    
    /**
     * Sorts a structured array by 'title' key (no # prefix).
     *
     * Callback for uasort().
     *
     * @param array $a
     *   First item for comparison. The compared items should be associative arrays
     *   that optionally include a 'title' key.
     * @param array $b
     *   Second item for comparison.
     *
     * @return int
     *   The comparison result for uasort().
     */
    public static function sortByTitleElement($a, $b) {
        return static::sortByKeyString($a, $b, 'title');
    }
    
    /**
     * Sorts a structured array by '#title' property.
     *
     * Callback for uasort().
     *
     * @param array $a
     *   First item for comparison. The compared items should be associative arrays
     *   that optionally include a '#title' key.
     * @param array $b
     *   Second item for comparison.
     *
     * @return int
     *   The comparison result for uasort().
     */
    public static function sortByTitleProperty($a, $b) {
        return static::sortByKeyString($a, $b, '#title');
    }
    
    /**
     * Sorts a string array item by an arbitrary key.
     *
     * @param array $a
     *   First item for comparison.
     * @param array $b
     *   Second item for comparison.
     * @param string $key
     *   The key to use in the comparison.
     *
     * @return int
     *   The comparison result for uasort().
     */
    public static function sortByKeyString($a, $b, $key) {
        $a_title = is_array($a) && isset($a[$key]) ? $a[$key] : '';
        $b_title = is_array($b) && isset($b[$key]) ? $b[$key] : '';
        return strnatcasecmp($a_title, $b_title);
    }
    
    /**
     * Sorts an integer array item by an arbitrary key.
     *
     * @param array $a
     *   First item for comparison.
     * @param array $b
     *   Second item for comparison.
     * @param string $key
     *   The key to use in the comparison.
     *
     * @return int
     *   The comparison result for uasort().
     */
    public static function sortByKeyInt($a, $b, $key) {
        $a_weight = is_array($a) && isset($a[$key]) ? $a[$key] : 0;
        $b_weight = is_array($b) && isset($b[$key]) ? $b[$key] : 0;
        return $a_weight <=> $b_weight;
    }

}

Members

Title Sort descending Modifiers Object type Summary
SortArray::sortByKeyInt public static function Sorts an integer array item by an arbitrary key.
SortArray::sortByKeyString public static function Sorts a string array item by an arbitrary key.
SortArray::sortByTitleElement public static function Sorts a structured array by &#039;title&#039; key (no # prefix).
SortArray::sortByTitleProperty public static function Sorts a structured array by &#039;#title&#039; property.
SortArray::sortByWeightElement public static function Sorts a structured array by the &#039;weight&#039; element.
SortArray::sortByWeightProperty public static function Sorts a structured array by &#039;#weight&#039; property.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.