Java的数组排序 [英] Java sort array
问题描述
我知道如何排序是有两列数组:
I know how to sort an array that has two columns:
Arrays.sort(myarray, new Comparator<String[]>() {
@Override
public int compare(String[] entry1, String[] entry2) {
String time1 = entry1[0];
String time2 = entry2[0];
return time2.compareTo(time1);
}
});
此排序由第一列中的arrray。
但是,如果我有什么更多的列?例如。
This sorts the arrray by the first column. But what if I have more columns? E.g.
myarray[0][0]= +3620205252
myarray[0][1]= 32534
myarray[0][2]= Franco Nera
myarray[0][3]= 183
myarray[1][0]= +3658300234
myarray[1][1]= 4334
myarray[1][2]= Judy Moira
myarray[1][3]= 28
等。
我想这个排序例如由第二列,或第四列...
我可以尝试通过与上述解决方案创建一个新的数组[1 + 3 + 4列] [第2列],然后排序它的工作了这一点,然后拆开元素,但就是太间接。
etc.. I want to sort this e.g. by the second column, or the fourth column... I can try to work this out by creating a new array[1st+3rd+4th column][2nd column] and then sort it with the above solution, then take the elements apart, but that is too circumstantial.
推荐答案
这是同样的事情有两列,但更多的列:您可以排序上尽可能多,或尽可能少,列,只要你想,只要你比较会检查您所关心的列。
It's the same thing as with two columns, but with more columns: you can sort on as many, or as few, columns as you want, as long as your comparator checks the columns you care about.
要通过的多个排序的你需要的列来决定比较顺序,并返回的compareTo
只要你有不平等列的结果。
To sort by multiple columns you need to decide the comparison order and return the compareTo
result as soon as you have unequal columns.
四角切圆,为什么你会使用这个数组?它的Java,还不如像使用一个对象或东西。
Tangentially, why would you use arrays for this? It's Java, might as well use like an object or something.
这篇关于Java的数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!