Suppose I have a preference for animals given by:
orderOfAnimals = {"dogs", "cats", "fish", "elephants", "giraffe", "hamsters", "poodles"};
I'm given many data with individual elements that look as follows:
exampleData = {{1, 2, "cats", 3}, {2, 4, "elephants", 6}, {6, 8,"dogs", 7}, {5, 7, "fish",7}}
For display purposes I want to order the data according to my preference of animals, but to keep track of which element is which. In this example:
reordering = {3, 1, 4, 2};
Grid[exampleData[[reordering]]]
In truth my animal preference is much more detailed, how could I efficiently calculate the reordering
list?
Update
Many thanks for current answers, I'm reviewing them. If it's easier the data can be modified into Rules, Associations or Dataset. It's the flexibility/efficiency of reordering that I'm interested in, it might be easier to operate on something like this:
assocData =Association @@ Thread[{"a", "b", "Animal", "c"} -> #] & /@ exampleData;
Answer Selection
Great answers, really appreciate them. The one I found most useful, and now exists (cited) in a deployed application, is Yves Klett. It was the simplest and most directly usable on my List
s and Association
s
gpap was helpful with associations, I agree about there being a better way to check multiple keys against an association - currently looking into this myself.
Kguler gets the reward for most solutions in one answer - almost won it for me on MapIndexed[Rule,expr].