[英]How to alphabetically sort two dimensional array in java
Lets say i have a two dimensional array:假设我有一个二维数组:
String [][] arr = {
{"bob","one"},
{"jack","two"},
{"adam","three"}
};
i would like to sort alphabetically according to column 0 so adam would be first the bob the jack - and it (or new array) will look like so:我想根据第 0 列按字母顺序排序,所以亚当首先是杰克的鲍勃 - 它(或新数组)看起来像这样:
{"adam","three"}
{"bob","one"},
{"jack","two"},
import java.util.Arrays;
import java.util.Comparator;
public class demo_sort {
public static void main(String[] args) {
final String[][] data = new String[][] {
new String[] {"bob","one"},
new String[] {"jack","two"},
new String[] {"adam","three"}
};
Arrays.sort(data, 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 : data) {
System.out.println(s[0] + " " + s[1]);
}
}
}
output输出
adam three亚当三
bob one鲍勃一号
jack two杰克二
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.