The default locale of sorted()

Ian Kelly ian.g.kelly at gmail.com
Mon Dec 3 17:34:29 EST 2012


On Mon, Dec 3, 2012 at 3:17 PM, Peng Yu <pengyu.ut at gmail.com> wrote:

> Hi,
>
> I'm not able to find the documentation on what locale is used for
> sorted() when the 'cmp' argument is not specified. Could anybody let
> me what the default is? If I always want LC_ALL=C, do I need to
> explicitly set the locale? Or it is the default?
>

The default is that a sorts before b if a < b.  So what you actually want
to know is how strings compare to one another, and the answer from the
Python 3 documentation is:

"Strings are compared lexicographically using the numeric equivalents (the
result of the built-in function
ord()<http://docs.python.org/3/library/functions.html#ord>)
of their characters."

If you want to sort according to a specific locale, use the locale.strxfrm
key function:

sorted_strings = sorted(original_strings, key=locale.strxfrm)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121203/157d0bef/attachment.html>


More information about the Python-list mailing list