编辑:我尝试使用camparator,但没有用,我收到错误
我必须读入包含内容的文件
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
这是我读到的代码
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;
}
如何根据文本文件中最后(或任何)数字列的第一列对 2D 数组进行排序?