在Java中对多维字符数组进行排序

发布于 2024-11-05 09:44:11 字数 765 浏览 1 评论 0原文

我有一个多维字符串数组过程[100][2],如下所示:

YB

CD

AB

BC

FE

EY

FD

YX

EG

我想在第一列字母上对其进行排序,以便最终结果如下:

AB

BC

CD

EY

EG

FE

FD

YB

YX 我尝试使用下面的代码,但这并不能解决问题:

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

        }
});

我得到的输出是:

AB

BC

CD

EY

FE

YB

EG

FD

YX

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江南烟雨〆相思醉 2024-11-12 09:44:11

以下单元测试演示了一个有效的 Comparator 实现。测试也会打印出结果。

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

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]);            
        }
    }
}
鹿童谣 2024-11-12 09:44:11

此代码(相同的比较器)按预期工作:

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

您的问题一定在其他地方。

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.

国际总奸 2024-11-12 09:44:11

您可能最好将每行的两个字符放在同一元素中。然后,当您需要单独的字符时,请使用

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

,您可以根据需要对一维数组进行排序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文