# What is the Alphabetical Inequality Test?

Mathematica can sort {"b","d","a","c"} into {"a","b","c","d"} with a simple application of Sort. What ordering function/command is being used?

It isn't <: "a" < "b" doesn't return True or False, just a < b.

I'm trying to sort a large data set that contains both strings and numeric values. An example:

data = {{9, 8, "b"}, {4, 2, "d"}, {0, 3, "a"}, {4, 9, "c"}}.

I can sort by the second element of each set:

Sort[data, #2[[2]] > #1[[2]] &] returns

{{4, 2, "d"}, {0, 3, "a"}, {9, 8, "b"}, {4, 9, "c"}}.

But Sorting by the third element doesn't work:

Sort[data, #2[[3]] > #1[[3]] &] returns data unchanged:

{{9, 8, "b"}, {4, 2, "d"}, {0, 3, "a"}, {4, 9, "c"}}.

It feels like a hack, but I can sort using Ordering and OrderedQ:

data[[Ordering[data[[All, 3]]]]] returns

{{0, 3, "a"}, {9, 8, "b"}, {4, 9, "c"}, {4, 2, "d"}}, as does

Sort[data, OrderedQ[{#1[[3]], #2[[3]]}] &].

Neither feels natural. Is there a lexicographical/alphabetical "inequality" command, or is it just OrderedQ?

You can use AlphabeticOrder:

data = {{9, 8, "b"}, {4, 2, "d"}, {0, 3, "a"}, {4, 9, "c"}};

Sort[
data,
AlphabeticOrder[ #1[[3]], #2[[3]] ]&
]

{{0, 3, "a"}, {9, 8, "b"}, {4, 9, "c"}, {4, 2, "d"}}

• That's it. I didn't seem to be using the right search terms online to find that command. Oct 26, 2018 at 12:15

AlphabeticOrder does not exist in version 10.1 which I use, but I don't believe it is necessary here. I argue that Order and OrderedQ are the more canonical (and certainly general) functions.

Additionally, where possible you should make use of SortBy (e.g. SortBy[data, #[[3]] &]) or Ordering (as you already did) rather than Sort, as these are more efficient methods.

An alternative to OrderedQ if you have need of Sort itself:

Sort[data, 0 < Order[#[[3]], #2[[3]]] &]
Sort[data, 0 > Order[#[[3]], #2[[3]]] &]
{{0, 3, "a"}, {9, 8, "b"}, {4, 9, "c"}, {4, 2, "d"}}
{{4, 2, "d"}, {4, 9, "c"}, {9, 8, "b"}, {0, 3, "a"}}

This is actually more concise than AlphabeticOrder.

• In law they say "lex specialis derogat legi generali": Sometimes the more special function has its merits and is maybe is less abstract? AlphabeticOrder on the other hand is more general than Order because English is not the only language out there... ;-)
– gwr
Oct 26, 2018 at 14:23
• @gwr I didn't notice that AlphabeticOrder handled different languages; that's nice! I have already voted for your answer. And thanks for the Latin lesson. Oct 26, 2018 at 14:32