Fixing numeric sorting for numbers with leading zeros
authorKaldari <rkaldari@wikimedia.org>
Thu, 20 Oct 2016 18:56:54 +0000 (11:56 -0700)
committerKaldari <rkaldari@wikimedia.org>
Thu, 20 Oct 2016 18:58:38 +0000 (11:58 -0700)
Bug: T148774
Change-Id: I34aa330645d9d82b6c4e57542e891dd2b36e42ad

includes/collation/NumericUppercaseCollation.php

index 4bf2f73..d15daec 100644 (file)
@@ -36,11 +36,13 @@ class NumericUppercaseCollation extends UppercaseCollation {
                // shorter numbers before longer ones; if identical, then the characters will be compared, which
                // generates the correct results for numbers of equal length.
                $sortkey = preg_replace_callback( '/\d+/', function ( $matches ) {
-                       $len = strlen( $matches[0] );
+                       // Strip any leading zeros
+                       $number = ltrim( $matches[0], '0' );
+                       $len = strlen( $number );
                        // This allows sequences of up to 65536 numeric characters to be handled correctly. One byte
                        // would allow only for 256, which doesn't feel future-proof.
                        $prefix = chr( floor( $len / 256 ) ) . chr( $len % 256 );
-                       return '0' . $prefix . $matches[0];
+                       return '0' . $prefix . $number;
                }, $sortkey );
 
                return $sortkey;