Liutauras Vilda wrote:Michael, hi.
What the output supposed to be if the matrix were:
A A A A
Z Y X W
X A D F
G V X G
Thank you so much for all your help.......
Piet Souris wrote:hi Michael,
was on vacation without internet, just got back. Glad we could help.
This is the one-liner that I mentioned, but on second thought the line was way too long, so I split it over three lines. .
Piet Souris wrote:
Michael Curley wrote: (...) This was my first approach.
well, it takes just one line of code to get the indices, after sorting the letters of the code word, but that involves some methods that are probably unfamiliar to you at this moment.
So, if you have a codeword with length L, then create an array A of length L, and initialize it with 0, 1, 2, ..., L - 1.
Now, if you sort your codeword and you swap charAt(i) with charAt(j), then also swap A[i] and A[j]. When done, the A array contains the sorted indices. Now, for each int row of your 2D array, make sure that element[i] will be equal to element[A[i]]. Be careful not to overwrite elements, though.
Piet Souris wrote:Hmmm...
if it were me, then I would produce a very simple int Comparator (that compares on the first elements), and do a sort on the transpose of the matrix.
But this is the Beginners Forum, and I don't think this exercise is easy at all. I would not use terms like 'very easy'. Let's wait for Michael to hear what he thinks.
Piet Souris wrote:Or, for something completely different: if your first row = "b a d c", and you sort it, then the original indices (0 1 2 3) will now be 1 0 3 2. What does that mean for the other rows?