# OrderedQ with string arguments

I don't find these results consistent:

OrderedQ[{"a", "A"}]

True

OrderedQ[{"a2", "A1"}]

False

Is there any explanation of that somewhere? (In fact it is not necessarily related to Mathematica, maybe there are some standards or established conventions about this.)

One could think that the explanation is: since "a" and "A" are equivalent
and as OrderedQ[{"A", "A"}] returns True, it's normal. But in that case OrderedQ[{"A", "a"}] shouldn't return False.

### EDIT

(Thanks to @Michael E2 comments)

It turns out that this question has nothing to do with the fact that "1" and "2" are digit characters. The same thing happens if one replaces "1" by "c" and "2" by "d" for example.

### EDIT2

This has been tested on Mathematica 11.3 and 5.1

• OrderedQ >> Details: By default, OrderedQ uses canonical order as described in the notes for Sort. _ and Sort >> Details: _Sort orders strings as in a dictionary, with uppercase versions of letters coming after lowercase ones.
– kglr
Oct 9, 2018 at 16:30
• @kglr This behaviour is quite puzzling to me because the ordering is clearly not lexicographic. By "lexicographic" I mean an ordering of strings of tokens that is based on an ordering of the tokens themselves. I.e., with pseudocode notation, string[1] > string2[1] implies that string1 > string2. This is clearly not the case here. Mathematica uses some more complex and more confusing ordering. Oct 9, 2018 at 19:00
• Also consider OrderedQ[{{"a", "2"}, {"A", "1"}}] --> True. If upper/lowecase is treated specially, does that mean that: (1) ordering is language dependent (consider Turkish dotted uppercase İ and dotless lowercase ı)? If yes, what language does M use? (2) ordering is not well-defined for certain scripts? Oct 9, 2018 at 19:02
• Example: Sort[{"I2", "İ2", "i2", "ı2", "I1", "İ1", "i1", "ı1"}] --> {"İ1", "İ2", "i1", "ı1", "I1", "i2", "ı2", "I2"}. My point is that the note in the documentation does not give an unambiguous description of what is going on. Oct 9, 2018 at 19:07
• IMO Mathematica sorting is under-documented; I created a question seeking better documentation but it has never been exhaustively or authoritatively answered. Oct 10, 2018 at 0:44

I think you have a good question. It seems that Sort treats "a" and "A" equivalently, and then sorts the elements that are equivalent. Here is an example that perhaps clarifies the issue:

Sort[{
"a2a1","A1a1","A2a1","a1a1","a2A1","A1A1","A2A1","a1A1",
"a2a2","A1a2","A2a2","a1a2","a2A2","A1A2","A2A2","a1A2"
}]

{"a1a1", "a1A1", "A1a1", "A1A1", "a1a2", "a1A2", "A1a2", "A1A2", "a2a1", "a2A1", "A2a1", "A2A1", "a2a2", "a2A2", "A2a2", "A2A2"}

Notice how the x1x1 terms come first, then the x1x2 terms, etc. Within each grouping the sort of the equivalent letters goes as "aa", "aA", "Aa", "AA" as expected.

• I would add that "dictionary order" is easier to understand with examples like OrderedQ[{"ac", "Ab"}] instead of the ones and twos. Also punctuation characters are treated differently (and different from my print dictionary) than letter-like characters, to which class digits apparently belong. Oct 9, 2018 at 20:24
• I don't understand the plain-English statement "It seems that Sort treats 'a' and 'A' equivalently" -- it does not in the most natural interpretation (to me) agree with the example below. Perhaps you could try phrasing that in a different way? Oct 10, 2018 at 0:51
• I think I have understood "It seems that Sort treats 'a' and 'A' equivalently" (see my answer) Oct 20, 2021 at 19:31