Example #1
0
 /**
  * Locale aware sorting, the key associations are kept, values are sorted alphabetically.
  *
  * @param array $arr array to be sorted (reference)
  * @param int $sortflag One of Collator::SORT_REGULAR, Collator::SORT_NUMERIC, Collator::SORT_STRING
  * @return void modifies parameter
  */
 public static function asort(array &$arr, $sortflag = null) {
     if (self::ensure_collator_available()) {
         if (!isset($sortflag)) {
             $sortflag = Collator::SORT_REGULAR;
         }
         self::$collator->asort($arr, $sortflag);
         return;
     }
     asort($arr, SORT_LOCALE_STRING);
 }
 /**
  * Locale aware sorting, the key associations are kept, values are sorted alphabetically.
  *
  * @param array $arr array to be sorted (reference)
  * @param int $sortflag One of collatorlib::SORT_NUMERIC, collatorlib::SORT_STRING, collatorlib::SORT_NATURAL, collatorlib::SORT_REGULAR
  *      optionally "|" collatorlib::CASE_SENSITIVE
  * @return bool True on success
  */
 public static function asort(array &$arr, $sortflag = collatorlib::SORT_STRING)
 {
     if (empty($arr)) {
         // nothing to do
         return true;
     }
     $original = null;
     $casesensitive = (bool) ($sortflag & collatorlib::CASE_SENSITIVE);
     $sortflag = $sortflag & ~collatorlib::CASE_SENSITIVE;
     if ($sortflag != collatorlib::SORT_NATURAL and $sortflag != collatorlib::SORT_STRING) {
         $casesensitive = false;
     }
     if (self::ensure_collator_available()) {
         if ($sortflag == collatorlib::SORT_NUMERIC) {
             $flag = Collator::SORT_NUMERIC;
         } else {
             if ($sortflag == collatorlib::SORT_REGULAR) {
                 $flag = Collator::SORT_REGULAR;
             } else {
                 $flag = Collator::SORT_STRING;
             }
         }
         if ($sortflag == collatorlib::SORT_NATURAL) {
             $original = $arr;
             if ($sortflag == collatorlib::SORT_NATURAL) {
                 foreach ($arr as $key => $value) {
                     $arr[$key] = self::naturalise((string) $value);
                 }
             }
         }
         if ($casesensitive) {
             self::$collator->setAttribute(Collator::CASE_FIRST, Collator::UPPER_FIRST);
         } else {
             self::$collator->setAttribute(Collator::CASE_FIRST, Collator::OFF);
         }
         $result = self::$collator->asort($arr, $flag);
         if ($original) {
             self::restore_array($arr, $original);
         }
         return $result;
     }
     // try some fallback that works at least for English
     if ($sortflag == collatorlib::SORT_NUMERIC) {
         return asort($arr, SORT_NUMERIC);
     } else {
         if ($sortflag == collatorlib::SORT_REGULAR) {
             return asort($arr, SORT_REGULAR);
         }
     }
     if (!$casesensitive) {
         $original = $arr;
         foreach ($arr as $key => $value) {
             $arr[$key] = textlib::strtolower($value);
         }
     }
     if ($sortflag == collatorlib::SORT_NATURAL) {
         $result = natsort($arr);
     } else {
         $result = asort($arr, SORT_LOCALE_STRING);
     }
     if ($original) {
         self::restore_array($arr, $original);
     }
     return $result;
 }