0

My String array looks like String[][] cat = new String[13][4];

I want to sort all [13] by the 3rd value in the second column "[][2]".

int  myNum = 0;
Arrays.sort(cat, new Comparator<String[]>() {
         @Override
         public int compare(final String[] entry1, final String[] entry2) {
         final String e1 = entry1[1];
         final String e2 = entry2[1];

         return e1.compareTo(e2);
         }
         });

         for (final String[] s : cat) {
         System.out.println("SORTING: "+s[0] + " " + s[1]+ " " + s[2]+ " " + s[3]);
         try {
                myNum = Integer.parseInt(s[2]);
            } catch (NumberFormatException nfe) {
                System.out.println("Could not parse " + nfe);
            }

            if (myNum > 3 && s[1].equals("1")) {
                moreValues2.add(s[1]);
                moreURLS2.add(s[3]);
            }

         } 

How do I choose what value to sort by. In my code above it is sorting by the first column but I wish to sort by the 3rd. I am not sure what to modify and why to modify it.
I read http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html to no avail. and version 7

2 Answers 2

1

In your comparator, the values are picked here :

final String e1 = entry1[1];
final String e2 = entry2[1];

Simply replace [1] with [2], or [3] (since I don't understand "third value in the second column").

Sign up to request clarification or add additional context in comments.

5 Comments

This is close, but since you are using a multidimensional array you will have to handle that somewhere, either in the compare function or in the function that calls it. Depends if you are passing the 2d array in or just the array from the 2d array (current code looks like it should do the latter)
I don't understand the difference between "the 2d array" and "the array from the 2d array". More precisely, "the array from the array" doesn't make sense to me.
OK entry1[x] where x is the column of the second array array[first array][second array]
"third value" would be the x[2] spot in zero based
Indeed, but then, column 2 is the third column, not the second one. Also, one more thing: your column 2 seems to contain integers. If you compare them using String comparison, you may not get what you expect ("9" > "33", for example). In that case, your comparator should parse the integers and compare them.
0

Instead of final String[] s : cat You can put final String s : cat[2] This will get all the strings in the 3rd row, and then you can sort them

2 Comments

If I understand correctly, the OP doesn't want to extract the elements of a row (or a column) and sort them, he wants to sort the whole array according to the value in a given column, without losing information.
Ah. Ok. I guess i didn't understand what he was asking

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.