From 3c8490b1e3206e88bd3b6491ebb0360ea01a98f8 Mon Sep 17 00:00:00 2001 From: Kaldari Date: Thu, 20 Oct 2016 11:56:54 -0700 Subject: [PATCH] Fixing numeric sorting for numbers with leading zeros Bug: T148774 Change-Id: I34aa330645d9d82b6c4e57542e891dd2b36e42ad --- includes/collation/NumericUppercaseCollation.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/includes/collation/NumericUppercaseCollation.php b/includes/collation/NumericUppercaseCollation.php index 4bf2f737fb..d15daec505 100644 --- a/includes/collation/NumericUppercaseCollation.php +++ b/includes/collation/NumericUppercaseCollation.php @@ -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; -- 2.20.1