Sorting and spaces.
Peter Otten
__peter__ at web.de
Thu May 31 12:53:58 EDT 2018
Tobiah wrote:
> I had a case today where I needed to sort two string:
>
> ['Awards', 'Award Winners']
>
> I consulted a few sources to get a suggestion as to
> what would be correct. My first idea was to throw them
> through a Linux command line sort:
>
> Awards
> Award Winners
>
> Then I did some Googling, and found that most US systems seem
> to prefer that one ignore spaces when alphabetizing. The sort
> program seemed to agree.
>
> I put the items into the database that way, but I had forgotten
> that my applications used python to sort them anyway. The result
> was different:
>
> >>> a = ['Awards', 'Award Winners']
> >>> sorted(a)
> ['Award Winners', 'Awards']
>
> So python evaluated the space as a lower ASCII value.
>
> Thoughts? Are there separate tools for alphabetizing
> rather then sorting?
>>> items = ["Awards", "Award Winners", "awards"]
>>> sorted(items)
['Award Winners', 'Awards', 'awards']
>>> import locale
>>> locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
'en_US.UTF-8'
>>> sorted(items, key=locale.strxfrm)
['awards', 'Awards', 'Award Winners']
>>> locale.setlocale(locale.LC_ALL, "C")
'C'
>>> sorted(items, key=locale.strxfrm)
['Award Winners', 'Awards', 'awards']
More information about the Python-list
mailing list