简体   繁体   中英

Sort 2D Array in java

EDIT: I tried using a camparator and it didnt work, I get an error

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?

I wouldn't use a 2D array for this purpose.

I would create a Team class and define instance variables for each of the fields in the file format. Then I would create a class that implements Comparator (say, TeamComparator ) that defines how to compare Teams based on whatever criteria is needed.

Then, you can have an array of Teams or a List of Teams .

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. You can use Arrays.sort() with a custom Comparator like this:

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) . If you want to sort the individual String arrays inside the 2D array, you'll have to sort each array individually.

EDIT: see the comments for a suggested compare method.

使用QuickSort对数组进行排序

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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