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 Sort
ing 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
?