How do you reorder a sublist of strings, within a list?
One way:
d /. x : {__String} :>
SortBy[x, StringReplace[#, {"A5" -> 1, "I" ~~ _ -> 2, "A" ~~ _ -> 3}] &];
% === newd
True
If you look at the output of StringReplace
you will see remaining StringExpression
heads, which is less than clean, but because they are all the same they do not affect the sort order.
A variation:
d /. x : {__String} :>
SortBy[x, Characters@# /. {{"A", "5"} -> 1, {"I", _} -> 2, {"A", _} -> 3} &];
% === newd
True
Map[Sort /* RotateLeft, d, {-2}]
% === newd
{{{"A5", "I4", "A2"}, {"A5", "I5", "A4"}}, {{"A5", "I4", "A2"}, {"A5", "I5", "A4"}}, {{"A5", "I4", "A2"}, {"A5", "I4", "A4"}}, {{"A5", "I4", "A2"}}, {{"A5", "I4", "A2"}}} True
Map[ReverseSortBy[#, {StringCases["A5"], StringCases[RegularExpression["[I\d]"]]}]&,d,{2}]
{{{A5, I4, A2}, {A5, I5, A4}}, {{A5, I4, A2}, {A5, I5, A4}}, {{A5, I4, A2}, {A5, I4, A4}}, {{A5, I4, A2}}, {{A5, I4, A2}}}
%==newd
True