1

I have a multi dimensional String array process[100][2] like following :

Y B

C D

A B

B C

F E

E Y

F D

Y X

E G

I want to sort it on the first column letter so that the final result will look so :

A B

B C

C D

E Y

E G

F E

F D

Y B

Y X I've tried using the below code but that does not do the trick :

Arrays.sort(process, new Comparator<String[]>() {
        @Override

        public int compare(final String[] entry1, final String[] entry2) {
                final String time1 = entry1[0];
                final String time2 = entry2[0];
                return time1.compareTo(time2);

        }
});

The output I get is :

A B

B C

C D

E Y

F E

Y B

E G

F D

Y X

3
  • 4
    In what way does it "not do the trick"? Is the array not sorted at all? Commented May 4, 2011 at 20:20
  • Can you show us definition of process array and how you check the results after calling Arrays.sort() method in your code? Commented May 4, 2011 at 20:36
  • See Peter's answer below. There's nothing wrong with your comparator. Commented May 4, 2011 at 20:51

3 Answers 3

1

The following unit test demonstrates a working Comparator implementation. The test prints out the result as well.

import java.util.Arrays;
import java.util.Comparator;

import junit.framework.TestCase;

public class ArrayTest extends TestCase {

    public class Sorter implements Comparator {
        public int compare(Object o1, Object o2){
            String[] arrayOne = (String[])o1;
            String[] arrayTwo = (String[])o2;
            return arrayOne[0].compareTo(arrayTwo[0]);
        }
    }

    public void testSort() {
        String[][] testData = {
                {"Y", "B"},
                {"C", "D"},
                {"A", "B"},
                {"B", "C"},
                {"F", "E"},
                {"E", "Y"},
        };

        Arrays.sort(testData, new Sorter());

        String[][] expectedOutput = {
                {"A", "B"},
                {"B", "C"},
                {"C", "D"},
                {"E", "Y"},
                {"F", "E"},
                {"Y", "B"},
        };

        for(int i = 0; i < testData.length; ++i) {            
            System.out.println(testData[i][0] + " " + testData[i][1]);
            assertEquals(expectedOutput[i][0], testData[i][0]);
            assertEquals(expectedOutput[i][1], testData[i][1]);            
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

The only real difference between your Comparator and his is that you're using the old, non-generic type and casting... which you shouldn't do in new code. There is in fact, nothing wrong with his comparator, it works fine.
Bear in mind this is intended to be a simple test case to demonstrate how to solve the questioners problem. That said, I'd still argue that there's nothing wrong with using 'vanilla' Java code as I've done.
It's not "vanilla" Java, it's "legacy" Java. Raw types should not be used in new code, plain and simple. As the warnings clearly tell you.
1

This code (identical comparator) works as expected:

    String[][] arr = {{"B","L"},{"C","M"},{"Z","N"}};

    Arrays.sort(arr, new Comparator<String[]>() {
        @Override
        public int compare(final String[] entry1, final String[] entry2) {
            final String time1 = entry1[0];
            final String time2 = entry2[0];
            return time1.compareTo(time2);
        }
    });

Your problem must be somewhere else.

Comments

0

You would probably be best off putting both of the characters in the same element for each row. Then, when you needed the separate characters, use

String firstCharacter = myString.charAt(0);
String secondCharacter = myString.charAt(1);

and you can sort your one-dimensional array however you like.

Comments

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.