How to decide alphabetical order when space is present? [closed]

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 5 years ago . for example, aa vs a a or PyTorch vs Py Discourse asked Aug 27, 2019 at 4:27 141 1 1 gold badge 1 1 silver badge 3 3 bronze badges

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.

Commented Aug 27, 2019 at 4:35

2 Answers 2

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)

answered Aug 27, 2019 at 7:32 Kate Bunting Kate Bunting 26.6k 4 4 gold badges 48 48 silver badges 68 68 bronze badges This is discussed frequently in style guides when it comes to sorting back matter reference lists. Commented Aug 28, 2019 at 2:07

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 
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.