Example #1
0
function ut_coll_sort($coll, &$arr, $sort_flag = Collator::SORT_REGULAR)
{
    return $GLOBALS['oo-mode'] ? $coll->sort($arr, $sort_flag) : collator_sort($coll, $arr, $sort_flag);
}
/**
 * Sorts an array, elements will be arranged from the lowest to the highest.
 * @param array $array					The input array.
 * @param int $sort_flag (optional)		Shows how elements of the array to be compared.
 * @param string $language (optional)	The language in which comparison is to be made. If language is omitted, interface language is assumed then.
 * @param string $encoding (optional)	The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
 * @return bool							Returns TRUE on success, FALSE on error.
 * Note: $sort_flag may have the following values:
 * SORT_REGULAR - internal PHP-rules for comparison will be applied, without preliminary changing types;
 * SORT_NUMERIC - items will be compared as numbers;
 * SORT_STRING - items will be compared as strings. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale;
 * SORT_LOCALE_STRING - items will be compared as strings depending on the current POSIX locale. If intl extension is enabled, then comparison will be language-sensitive using internally a created ICU locale.
 * This function is aimed at replacing the function sort() for sorting human-language strings.
 * @link http://php.net/manual/en/function.sort.php
 * @link http://php.net/manual/en/collator.sort.php
 */
function api_sort(&$array, $sort_flag = SORT_REGULAR, $language = null, $encoding = null)
{
    if (INTL_INSTALLED) {
        if (empty($encoding)) {
            $encoding = _api_mb_internal_encoding();
        }
        $collator = _api_get_collator($language);
        if (is_object($collator)) {
            if (api_is_utf8($encoding)) {
                $sort_flag = $sort_flag == SORT_LOCALE_STRING ? SORT_STRING : $sort_flag;
                return collator_sort($collator, $array, _api_get_collator_sort_flag($sort_flag));
            } elseif ($sort_flag == SORT_STRING || $sort_flag == SORT_LOCALE_STRING) {
                global $_api_collator, $_api_encoding;
                $_api_collator = $collator;
                $_api_encoding = $encoding;
                return usort($array, '_api_cmp');
            }
        }
    }
    return sort($array, $sort_flag);
}