4
$\begingroup$

Define the order of sorting as 2>A>K>Q>J>10>9>8>7>6>5>4>3.

How can I reorder each of the three sets of data in the list from smallest to largest according to this order of magnitude?

{{"10", "4", "3", "K", "K", "3", "4", "5", "6", "7", "8", "9", "10", 
  "3", "3"}, {"3", "K", "3", "4", "5", "6", "7", "8", "9", "10", "4", 
  "4", "4", "4", "3"}, {"4", "5", "6", "7", "8", "9", "2", "5", "5", 
  "5", "5", "3", "A", "5", "6"}}
$\endgroup$
1
  • 1
    $\begingroup$ As your game develops, perhaps of interest will be the PlayingCardGraphic function. $\endgroup$
    – Syed
    Commented Feb 8 at 10:04

3 Answers 3

7
$\begingroup$
mysortingList = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", 
   "5", "4", "3"};

data = {
 {"10", "4", "3", "K", "K", "3", "4", "5", "6", "7", "8", "9", "10", 
  "3", "3"}, 
 {"3", "K", "3", "4", "5", "6", "7", "8", "9", "10", "4", 
  "4", "4", "4", "3"}, 
 {"4", "5", "6", "7", "8", "9", "2", "5", "5", 
  "5", "5", "3", "A", "5", "6"}
 };

SortBy[#, Position[mysortingList, #] &] & /@ data

(* {{"K", "K", "10", "10", "9", "8", "7", "6", "5", "4", "4", "3", "3", "3", "3"}, {"K", "10", "9", "8", "7", "6", "5", "4", "4", "4", "4", "4", "3", "3", "3"}, {"2", "A", "9", "8", "7", "6", "6", "5", "5", "5", "5", "5", "5", "4", "3"}} *)

$\endgroup$
6
$\begingroup$

Using PositionIndex:

lists = {{"10", "4", "3", "K", "K", "3", "4", "5", "6", "7", "8", "9",
    "10", "3", "3"}, {"3", "K", "3", "4", "5", "6", "7", "8", "9", 
   "10", "4", "4", "4", "4", "3"}, {"4", "5", "6", "7", "8", "9", "2",
    "5", "5", "5", "5", "3", "A", "5", "6"}};

ord = {"2", "A", "K", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
pi = PositionIndex[ord];

SortBy[#, pi] & /@ lists // Grid

enter image description here

$\endgroup$
5
$\begingroup$
MapIndexed[(func[#] = #2) &, 
  ToString /@ Unevaluated@(2 > A > K > Q > J > 10 > 9 > 8 > 7 > 6 > 5 > 4 > 3)];

SortBy[func] /@ 
    {{"10", "4", "3", "K", "K", "3", "4", "5", "6", "7", "8", "9", "10", "3", "3"}, 
     {"3", "K", "3", "4", "5", "6", "7", "8", "9", "10", "4", "4", "4", "4", "3"}, 
     {"4", "5", "6", "7", "8", "9", "2", "5", "5", "5", "5", "3", "A", "5", "6"}}

(*
{{"K", "K", "10", "10", "9", "8", "7", "6", "5", "4", "4", "3", "3", "3", "3"}, 
 {"K", "10", "9", "8", "7", "6", "5", "4", "4", "4", "4", "4", "3", "3", "3"}, 
 {"2", "A", "9", "8", "7", "6", "6", "5", "5", "5", "5", "5", "5", "4", "3"}}
 *)
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.