[wp-trac] [WordPress Trac] #16530: Implement locale based sorting

WordPress Trac wp-trac at lists.automattic.com
Sat Feb 12 02:13:17 UTC 2011

#16530: Implement locale based sorting
 Reporter:  cyberskull   |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  I18N         |     Version:  3.1
 Severity:  normal       |  Resolution:
 Keywords:               |

Comment (by cyberskull):

 Replying to [comment:4 cyberskull]:
 > Replying to [comment:3 Denis-de-Bernardy]:
 > > It's a tiny bit trickier, actually:
 > >
 > > http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-
 > I was deliberately ignoring sorting numbers at this point. I wanted more
 to get a framework in place to handle natural locale comparisons. With the
 framework in place, we could start plugging in functions for each language
 to do the sorts.
 > I wonder if there is a natural sort PHP package?

 '''Good news everybody!'''

 PHP has a natural sort functions built in.
 [http://www.php.net/manual/en/function.natsort.php natsort] for arrays and
 [http://php.net/manual/en/function.strnatcmp.php strnatcmp] for a natural
 string comparison.

 So to update my examples from above:

 function local_compare($a, $b)
         local function $locale_format = $locale_sorts{locale()};
         #Gets the appropriate formatter function from a hash of functions
         #on the locale() or returns null/undef.

                 $a = $locale_format($a); #format a
                 $b = $locale_format($b); #format b

         return strnatcmp($a, $b);

 And the formatter:
         $string =~ /^((a|the)\s+)?(.+)/i;
         return $3;

Ticket URL: <http://core.trac.wordpress.org/ticket/16530#comment:5>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software

More information about the wp-trac mailing list