开发者

Sorting a multi dimensional character array in Java

开发者 https://www.devze.com 2023-03-03 02:12 出处:网络
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

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


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]);            
        }
    }
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消