From f1e96a06d8471dd10b730bf89747665eb4595211 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Fri, 5 Feb 2016 16:10:26 +0100 Subject: [PATCH] Fixes #456: Tag cloud does not sort tags (fully) alphabetically * Use Collator class to sort tags using the locale (in PECL intl, included in most PHP installation). * Use strcasecmp if Collator is not found. Both sorts are case insensitive. --- index.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/index.php b/index.php index 31dcbf0..93e4cc6 100644 --- a/index.php +++ b/index.php @@ -1203,11 +1203,25 @@ function renderPage() // We sort tags alphabetically, then choose a font size according to count. // First, find max value. - $maxcount=0; foreach($tags as $key=>$value) $maxcount=max($maxcount,$value); - ksort($tags); + $maxcount = 0; + foreach ($tags as $value) { + $maxcount = max($maxcount, $value); + } + + // Sort tags alphabetically: case insensitive, support locale if avalaible. + uksort($tags, function($a, $b) { + // Collator is part of PHP intl. + if (class_exists('Collator')) { + $c = new Collator(setlocale(LC_ALL, 0)); + return $c->compare($a, $b); + } else { + return strcasecmp($a, $b); + } + }); + $tagList=array(); foreach($tags as $key=>$value) - // Tag font size scaling: default 15 and 30 logarithm bases affect scaling, 22 and 6 are arbitrary font sizes for max and min sizes. + // Tag font size scaling: default 15 and 30 logarithm bases affect scaling, 22 and 6 are arbitrary font sizes for max and min sizes. { $tagList[$key] = array('count'=>$value,'size'=>log($value, 15) / log($maxcount, 30) * (22-6) + 6); }