For example, here is a list
{{1, "u"}, {6, "d"}, {3, "u"}, {4, "d"}, {2, "u"}, {5, "u"}, {3,
"d"}, {1, "d"}, {4, "u"}, {2, "d"}, {5, "d"}, {6, "u"}}
The default canonical order for integer is 1,2,3,4
The default canonical order for letters is a,b,c,d,
But now I want to sort this list according to canonical integer order while "u" should be ordered before "d", how to achieve this easily?
The example list I gave here has a good structure, so can be solved with specific tricks. But the problem is general. How to sort list of mixed types of elements with each type a specified canonical order?
SortBy
orKeySortBy
after casting theList
as anAssociation
(which I expect is faster, but haven't verified empirically). $\endgroup$SortBy[list, {First, Switch[#[[2]], "u", -1, "d", 1] &}]
for your particular example works. $\endgroup$