Interesting problem. I think I have a working solution.
My thinking was that we can encode the vectors into a matrix to track which letters must come before and after each other letter by logic. Then we should be able to sort that matrix to find a working order.
Here, I take the three vectors and encode their implied ordering using nested loops.
v1 <- c("A","G","F","C","D","D")
v2 <- c("A","G","F","E","C")
v3 <- c("B", "A","G")
vecs <- list(v1, v2, v3)
unique_ltrs <- unique(unlist(vecs))
ltr_len <- length(unique_ltrs)
m <- matrix(0, nrow = ltr_len, ncol = ltr_len,
dimnames = list(unique_ltrs, unique_ltrs))
# Loops to populate m with what we know
for (v in 1:length(vecs)) {
vec <- unique(unlist(vecs[v]))
for (l in 1:length(vec)) {
for (l2 in 1:length(vec)) {
m_pos <- c(match(vec[l], unique_ltrs),
match(vec[l2], unique_ltrs))
compare <- ifelse(l < l2, -1, ifelse(l2 < l, 1, 0))
m[m_pos[1], m_pos[2]] <- compare
}
}
}
Here, 1 indicates the column letter comes before the row letter, while -1 means the row comes first.
> m
A G F C D E B
A 0 -1 -1 -1 -1 -1 1
G 1 0 -1 -1 -1 -1 1
F 1 1 0 -1 -1 -1 0
C 1 1 1 0 -1 1 0
D 1 1 1 1 0 0 0
E 1 1 1 -1 0 0 0
B -1 -1 0 0 0 0 0
Then we sort the matrix (relying on the code here), and a working order appears in the rownames:
m_ord <- m[do.call(order, as.data.frame(m)),]
#> m_ord
# A G F C D E B
#B -1 -1 0 0 0 0 0
#A 0 -1 -1 -1 -1 -1 1
#G 1 0 -1 -1 -1 -1 1
#F 1 1 0 -1 -1 -1 0
#E 1 1 1 -1 0 0 0
#C 1 1 1 0 -1 1 0
#D 1 1 1 1 0 0 0
rownames(m_ord)
#[1] "B" "A" "G" "F" "E" "C" "D"