One possible way I can think of, though probably not the most efficient:
Im going to use your example to explain:
A B D
A B C
A C D
create a an array of the unique characters, so you would get (for example):
A B D C
Also you should probably have a enum to describe the possible relationships
enum Type
{
Unknown = 0,
Greater = 1,
Equal = 2,
Less = 3,
}
now, create a square matrix whose dimensions are the same as the above array, default everything to 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 into the matrix when you are figuring out the ordering. To see what i mean, look here:
A B D C
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 populate the matrix, loop through each input array and get each character and the 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 locations in the grid everytime, because when one char is less than another, it also means the second char is greater than the first.
Now you would simply insert the chars into an array one at a time (Linked List would be the easiest, you will see why in a second)
Each time you insert a char, you would start at the begining of your return array, and iterate through, looking up the indexes in the first array, and checking the matrix for a Type.Greater or a Type.Less (Depends on which way you are comparing, curr char to array, or array to current char) and insert it if you encounter a value different than what you expected.
If you hit a Type.Unknown in the matix during your insertion, you know you don't have enough info to sort these arrays, if you hit a Type.Equal, you can ignore inserting this char (assuming you don't want duplicates in your result.)
And then you would output your return array