Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  /**
   3   * Helper class for sorting arrays on arbitrary criteria for usort/uasort.
   4   *
   5   * Copyright 2003-2017 Horde LLC (http://www.horde.org/)
   6   *
   7   * See the enclosed file LICENSE 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      /**
  27       * Compare two associative arrays by the array key defined in self::$key.
  28       *
  29       * @param array $a
  30       * @param array $b
  31       */
  32      public function compare($a, $b)
  33      {
  34          return strcoll(Horde_String::lower($a[$this->key], true, 'UTF-8'), Horde_String::lower($b[$this->key], true, 'UTF-8'));
  35      }
  36  
  37      /**
  38       * Compare, in reverse order, two associative arrays by the array key
  39       * defined in self::$key.
  40       *
  41       * @param scalar $a  TODO
  42       * @param scalar $b  TODO
  43       *
  44       * @return TODO
  45       */
  46      public function reverseCompare($a, $b)
  47      {
  48          return strcoll(Horde_String::lower($b[$this->key], true, 'UTF-8'), Horde_String::lower($a[$this->key], true, 'UTF-8'));
  49      }
  50  
  51      /**
  52       * Compare array keys case insensitively for uksort.
  53       *
  54       * @param scalar $a  TODO
  55       * @param scalar $b  TODO
  56       *
  57       * @return TODO
  58       */
  59      public function compareKeys($a, $b)
  60      {
  61          return strcoll(Horde_String::lower($a, true, 'UTF-8'), Horde_String::lower($b, true, 'UTF-8'));
  62      }
  63  
  64      /**
  65       * Compare, in reverse order, array keys case insensitively for uksort.
  66       *
  67       * @param scalar $a  TODO
  68       * @param scalar $b  TODO
  69       *
  70       * @return TODO
  71       */
  72      public function reverseCompareKeys($a, $b)
  73      {
  74          return strcoll(Horde_String::lower($b, true, 'UTF-8'), Horde_String::lower($a, true, 'UTF-8'));
  75      }
  76  
  77  }