Что-то быстрое и грязное, разбитое на маленькие шаги (для консоли R):
library(stringr)
ab <- paste(a, b, sep = ",")
ab_split <- strsplit(ab, " *,")[[1]]
ab_split_ordered <- ab_split[order(str_extract(ab_split, "[A-Z]"), str_extract(ab_split, "[0-9]"))]
ab_split_ordered_spaced <- ifelse(grepl(">", ab_split_ordered), paste0(" ", ab_split_ordered), ab_split_ordered)
cat(ab_split_ordered_spaced, sep = "\n")
1<--A
A-->1
A-->2
A-->3
A-->4
A-->5
A-->6
1<--B
B-->1
B-->2
1<--C
C-->1
C-->3
1<--D
Данные
a <- "1<--A ,1<--B ,1<--C ,1<--D"
b <- "A-->1 ,A-->2 ,A-->3 ,A-->4 ,A-->5 ,A-->6 ,B-->1 ,B-->2,C-->1,C-->3"
Обновленный пример:
library(stringr)
ab <- paste(a, b, sep = ",")
ab_split <- strsplit(ab, " *,")[[1]]
ids <- str_extract(ab_split, "^[^<-]+(?=-)|(?<=-)[^>-]+$")
values <- str_extract(ab_split, "^[^<-]+(?=<)|(?<=>)[^>-]+$")
ab_split_ordered <- ab_split[order(ids, values)]
ab_split_ordered_spaced <- ifelse(grepl(">", ab_split_ordered), paste0(strrep(" ", max(nchar(values)) + 3), ab_split_ordered), ab_split_ordered)
cat(ab_split_ordered_spaced, sep = "\n")
112-->1
1111<--112
112-->2
AA1-->11
1111<--AA1
AA1-->222
AA1-->3
AA1-->4
AA1-->5
AA1-->6
C1-->1
1111<--C1
C1-->3
1111<--DD
Данные:
a <- "1111<--AA1 ,1111<--112 ,1111<--C1 ,1111<--DD"
b <- "AA1-->11 ,AA1-->222 ,AA1-->3 ,AA1-->4 ,AA1-->5 ,AA1-->6 ,112-->1 ,112-->2,C1-->1,C1-->3"