3

for example,

aa

vs

a a

or

PyTorch

vs

Py Discourse

1
  • 2
    The alphabet has no spaces in it so they can play no part in an alphabetical ordering: ignore them. And this is a programming question, so belongs on Stack Overflow so please reask your question there. But seriously you should just use the default Unicode Collation Algorithm comparisons and sorting: it always gets this right. Most people fail terribly.
    – tchrist
    Commented Aug 27, 2019 at 4:35

2 Answers 2

7

There are two forms of alphabetical sorting, 'letter by letter' and 'word by word'. In the first you ignore spaces, so Newbury would file before New Zealand. In 'word by word', all phrases beginning with a particular word are filed together, so New Brunswick, New York, New Zealand come before Newbury. However, Py Discourse would file before PyTorch in either method.

Kate (former library cataloguer)

1
  • This is discussed frequently in style guides when it comes to sorting back matter reference lists. Commented Aug 28, 2019 at 2:07
3

tchrist claims in a comment to the question that this is a programming question. I disagree. vainaijr asks what order to put strings in, not whether this should be done by programming a computer or by whatever other means.

Space precedes anything else, and so precedes any letter. Thus

Py Z
pyramid
pyramid selling
Pyramiden
python

or

Will, H.
Will, J.
Will, T.
Williams, N.
Williams, R.
Williamson
Wills

Some ignore spaces and punctuation when sorting strings into alphabetical order. That would result in:

Will, H.
Williams, N.
Williamson
Williams, R.
Will, J.
Wills
Will, T.

I would not recommend this, because it misleads the reader. If I were looking in an index for a Williams, and found a block of entries, each entry a Williams, and saw that my Williams was not there, I'd conclude that it was not in the index at all. I wouldn't expect it to be in another block of Williamses, separated from the other block by a Williamson.

Not the answer you're looking for? Browse other questions tagged or ask your own question.