]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Fixes #456: Tag cloud does not sort tags (fully) alphabetically 461/head
authorArthurHoaro <arthur@hoa.ro>
Fri, 5 Feb 2016 15:10:26 +0000 (16:10 +0100)
committerArthurHoaro <arthur@hoa.ro>
Fri, 5 Feb 2016 15:10:34 +0000 (16:10 +0100)
  * 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

index 31dcbf0fe7455a9de4a437ea190d556801734117..93e4cc63f503d6eae03952ec3ce7a0a8f3f74857 100644 (file)
--- 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);
         }