# Tag Info

14

SortBy[l, {First@#, -ToCharacterCode@Last@#} &] (*{{1, "u"}, {1, "d"}, {2, "u"}, {2, "d"}, {3, "u"}, {3, "d"}, {4, "u"}, {4, "d"}, {5, "u"}, {5, "d"}, {6, "u"}, {6, "d"}}*) Or the same, but slightly shorter code SortBy[l, {#, -ToCharacterCode@#2} &@@#&] Edit The following uses the same sorting strategy but is much faster (by using this): ...

7

A simple approach giving the requested result is SortBy[list, (#[[2]] /. {"u" -> 0, "d" -> 1}) + 100 #[[1]] &] (* {{1, "u"}, {1, "d"}, {2, "u"}, {2, "d"}, {3, "u"}, {3, "d"}, {4, "u"}, {4, "d"}, {5, "u"}, {5, "d"}, {6, "u"}, {6, "d"}} *) In fact, even simpler is SortBy[list, (# /. {"u" -> 0, "d" -> 1}) &] Simpler yet is ...

7

Using Ordering it's possible to do this. Ordering can be seen as a permutation that brings a list to the identity permutation. And applying Ordering again to this permutation we get the inverse permutation that brings a list from identity to the original order. Using these observations: OrderingToTarget[list_, sourceIds_, targetIds_] := list[[Ordering @ ...

4

Your limited example implies that you want to reverse the canonical order for the letters in position 2 so that "u" will appear before "d". I've assumed that all letters of the alphabet could appear in position 2 so if that is the case then this would do it: Flatten[Reverse[GatherBy[Sort[list], First], 2], 1] ...but there is probably a more straight ...

4

NA = Missing["NotAvailable"]; SM = 10^-9.; The situation arises when you make a reverse sort like this one data = {{"Libya", 1, 2, NA, 3}, {"Belgium", NA, 10, 30 , 20}, {"Egypt", NA, NA, 8, 7}, {"USA", 21, 18, 18, 17}}; Reverse @ SortBy[data, #[[2]] &] // TableForm Solution: Temporarily replace missing data with a number small enough to not occur ...

2

I think a general solution is to perform the first test followed by the second test in case of ties. Sort[list, first[test] || tie[first] && second[test]] For your specific example 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"}} With the integers sorted on ...

2

START EDIT: "Sort by default orders integers, rational, and approximate real numbers by their numerical values." Since your expressions are none of these, you should expect the "canonical order" to likely be other than numeric. "In most cases, NumericQ[expr] gives True whenever N[expr] yields an explicit number"; consequently, NumericQ will return True for ...

1

Grid[ Transpose[{ {"X-Rays:", "6.3 MeV:", "4.3 MeV:", "2.2 MeV:"}, Cases[Catenate@list, {Rule[_, a_?Positive]} :> Round[a, 0.01]], Array["Gy" &, Length@cas]}], Frame -> True, Spacings -> {2, 1.5}, Alignment -> {{Left, Right}}]

1

TableForm@Transpose@ { {"X-Rays:", "6.3 MeV:", "4.3 MeV:"} , ToString[#] <> " Gy" & /@ Select[Dose /. Join @@ lst, Positive] } To get the rounded numbers as you have displayed, replace the Select expression with Select[Round[Dose /. Join @@ lst, 0.01], Positive]

Only top voted, non community-wiki answers of a minimum length are eligible