'Sort values of named vector A by vector B but keeping the order of names where possible [duplicate]

I have a named vector like that:

my_vec <- c("Max"= "England", "Manfred"= "Germany", "Ingolf"= "Germany", "Paul"= "England", "Peter"= "Germany", "Nina"= "Italy")

I need to sort this vector by countries as specified in another vector while keeping the order of the persons for a given country as before. So if the ordering of the countries is...

sort_scheme <- c("England", "Germany", "Italy")

... then my desired output is:

goal_vec <- c("Max"= "England", "Paul"= "England", "Manfred"= "Germany", "Ingolf"= "Germany", "Peter"= "Germany", "Nina"= "Italy")

As you can see the desired output has the same order of countries as in sort_scheme (England first, then Germany, then Italy) and, inside one country the original order of names is the same (for England Max comes first then Paul, and so on). The solution should be flexible for other characters (new names and countries) with base R. All I've come up with is incredibly cumbersome.

Edit I found this being a duplicate and voted to close.



Solution 1:[1]

We can use order:

my_vec[order(match(my_vec, sort_scheme))]

      Max      Paul   Manfred    Ingolf     Peter      Nina 
"England" "England" "Germany" "Germany" "Germany"   "Italy" 

As per @MerijnvanTilborg - if "nested" ordering using the vector names is required (which is not the case in the OP) we can use: my_vec[order(match(my_vec, sort_scheme), names(my_vec))]

See ?order:

In the case of ties in the first vector, values in the second are used to break the ties.

Solution 2:[2]

Sorting a named value sorts the vector by its values and not its names. Assuming your sort_scheme is alphabetically from A-Z you can just use sort on your vector.

sort(my_vec)
#       Max      Paul   Manfred    Ingolf     Peter      Nina 
# "England" "England" "Germany" "Germany" "Germany"   "Italy" 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Merijn van Tilborg