One possible way that I can think of, although probably not the most effective:
I am going to use your example to explain:
ABD ABC ACD
create an array of unique characters so you get (for example):
ABDC
You should also have an enumeration to describe possible relationships
enum Type { Unknown = 0, Greater = 1, Equal = 2, Less = 3, }
Now create a square matrix, the sizes of which coincide with the array indicated above, by default everything is for Type.Unknown.
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Your array will serve as an index in the matrix when you determine the order. To see what I mean, look here:
ABDC A 0 0 0 0 B 0 0 0 0 D 0 0 0 0 C 0 0 0 0
Go through and make the diagonal as Type.Equal
2 0 0 0 0 2 0 0 0 0 2 0 0 0 0 2
Now you need to fill in the matrix, pass through each input array and get each character and one after it. Use these 2 characters, find the index in the matrix for each (using your array) and update the matrix.
for(int i=0; i<len(arr)-1; i++) { char c1 = arr[i], c2 = arr[i+1]; int i1,i2; //get indices of characters in array and put in i1, and i2 respectively matrix[i1][i2] = Type.Less; matrix[i2][i1] = Type.Greater }
you assign 2 places in the grid every time, because when one char is smaller than the other, it also means that the second char is larger than the first.
Now you just insert the characters into the array one at a time (Linked List will be the easiest, you will see why in a second)
Each time you insert a char, you start at the beginning of your returned array and iterate, look at the indices in the first array and check the matrix for Type.Greater or Type.Less (Depends on how you compare curr char with an array or an array with current char) and paste it if you encounter a value other than expected.
If you press Type.Unknown in matix during your insertion, you know that you do not have enough information to sort these arrays, if you click on Type.Equal, you can ignore the insertion of this char (if you do not want to duplicate the result.)
And then you will output your return array