简体   繁体   English

在Java中对2D数组进行排序

[英]Sort 2D Array in java

EDIT: I tried using a camparator and it didnt work, I get an error 编辑:我尝试使用一个camparator,它没有用,我得到一个错误

I have to read in a file with the contents 我必须阅读包含内容的文件

2011 Regular Season
Boston 162 5710 875 1600 352 35 203
NY_Yankees 162 5518 867 1452 267 33 222
Texas 162 5659 855 1599 310 32 210
Detroit 162 5563 787 1540 297 34 169
St.Louis 162 5532 762 1513 308 22 162
Toronto 162 5559 743 1384 285 34 186
Cincinnati 162 5612 735 1438 264 19 183
Colorado 162 5544 735 1429 274 40 163
Arizona 162 5421 731 1357 293 37 172
Kansas_City 162 5672 730 1560 325 41 129

this is the code I read it in with 这是我读过的代码

static String specstat[][] = new String[1000][1000];

public static void main(String args[]) {

Arrays.sort(specstat, 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);
        }
    });

    for (final String[] s : specstat) {
        System.out.println(s[0] + " " + s[0]);
    }


    execute();
}

public static String[][] execute() {
    int line = 0;
    try {
        BufferedReader in = new BufferedReader(new FileReader(
                "files/input.txt"));

        String str;
        while ((str = in.readLine()) != null) {
            if (str.trim().length() == 0) {
                continue; // Skip blank lines
            }
            specstat[line] = str.split("\\s+");
            line++;
        }
        in.close();
    } catch (IOException e) {
        System.out.println("Can not open or write to the file.");
    }
    return specstat;

}

How can I sort the 2D Array based off of the first column of the last (or any) column of numbers from the text file? 如何根据文本文件中数字的最后一列(或任何一列)的第一列对2D数组进行排序?

I wouldn't use a 2D array for this purpose. 我不会为此目的使用2D阵列。

I would create a Team class and define instance variables for each of the fields in the file format. 我将创建一个Team类,并以文件格式为每个字段定义实例变量。 Then I would create a class that implements Comparator (say, TeamComparator ) that defines how to compare Teams based on whatever criteria is needed. 然后,我将创建一个实现Comparator的类(例如TeamComparator ), TeamComparator定义了如何根据所需条件比较Teams

Then, you can have an array of Teams or a List of Teams . 然后,您可以拥有一组TeamsTeams List

Last, you can sort the array with 最后,您可以使用

Arrays.sort(teamsArray, new TeamComparator()) 

or the list with 或带有的列表

Collections.sort(teamsList, new TeamComparator())

From what I'm understanding, you want to sort the 2D array by arranging the positions of the arrays inside of them. 据我了解,您想通过排列2D数组在其中的位置来对它们进行排序。 You can use Arrays.sort() with a custom Comparator like this: 您可以将Arrays.sort()与自定义Comparator如下所示:

Arrays.sort(specstat, new Comparator<String[]>() {
    @Override
    public int compare(String[] array1, String[] array2) {
        //do your comparison here...
    }
});

It may help to use String.compare() or compare numerical values using Integer.parseInt(String s) . 使用String.compare()或使用Integer.parseInt(String s)比较数值可能会有所帮助。 If you want to sort the individual String arrays inside the 2D array, you'll have to sort each array individually. 如果要在2D数组中对单个String数组进行排序,则必须对每个数组进行单独排序。

EDIT: see the comments for a suggested compare method. 编辑:请参阅注释,以获取建议的比较方法。

使用QuickSort对数组进行排序

您可以将Arrays.sort与比较器一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM