[英]Understanding sorting multidimensional array with comarator
My String array looks like String[][] cat = new String[13][4]; 我的String数组看起来像String [] [] cat = new String [13] [4];
I want to sort all [13] by the 3rd value in the second column "[][2]". 我想按第二列“ [] [2]”中的第三个值对所有[13]进行排序。
int myNum = 0;
Arrays.sort(cat, new Comparator<String[]>() {
@Override
public int compare(final String[] entry1, final String[] entry2) {
final String e1 = entry1[1];
final String e2 = entry2[1];
return e1.compareTo(e2);
}
});
for (final String[] s : cat) {
System.out.println("SORTING: "+s[0] + " " + s[1]+ " " + s[2]+ " " + s[3]);
try {
myNum = Integer.parseInt(s[2]);
} catch (NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
if (myNum > 3 && s[1].equals("1")) {
moreValues2.add(s[1]);
moreURLS2.add(s[3]);
}
}
How do I choose what value to sort by. 如何选择要排序的值。 In my code above it is sorting by the first column but I wish to sort by the 3rd. 在上面的代码中,它按第一列排序,但我希望按第三列排序。 I am not sure what to modify and why to modify it. 我不确定要修改什么以及为什么要修改它。
I read http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html to no avail. 我没有阅读http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html 。 and version 7 和版本7
In your comparator, the values are picked here : 在比较器中,在这里选择值:
final String e1 = entry1[1];
final String e2 = entry2[1];
Simply replace [1] with [2], or [3] (since I don't understand "third value in the second column"). 只需将[1]替换为[2]或[3](因为我不理解“第二列中的第三个值”)。
代替final String[] s : cat
可以将final String s : cat[2]
放到第三行,然后对它们进行排序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.