I have a task in which I have a labeled numeric vector, say size 5 and labels a, b, c, d, e, and I need to sort it and then print the labels in (inverse) sorted order. So for example, given this vector v1 in input:
a b c d e
1 -3 -1 10 5 -15
the required output would be: cdbae.
Now, the hard part is handling ties. I need to print all possible orders in case of a tie. So for example, given this other vector v2 in input:
a b c d e
1 10 29 10 10 -15
After sorting we’d have:
b a c d e
1 29 10 10 10 -15
But of course we have 3! = 6 possible permutations. I would like it to print this array:
v <- c("bacde", "badce", "bcade", "bcdae", "bdcae", "bdace").
If that helps, the number of labels is never more than 10 so I don’t mind performance relative to that, for that matter.
This does it. But I wouldn’t recommend running on dataset with too many ties.
Of course it handles multiple ties as well. Running on:
gives: