[英]How should java comparator class be declared to sort the array by there second element in ascending and descending order
int arr[][] = new int[][] {
{3, 7},
{1, 2},
{8, 9},
{4, 2},
{5, 3},
{0,90}
};
this is my array ,i can sort it ascending or descending order based on there first element using bellow code :这是我的数组,我可以使用下面的代码根据第一个元素对它进行升序或降序排序:
Arrays.sort(arr,(a,b)->Integer.compare(a[0], b[0]));
Now my question is how to do the same thing using second element on the array.现在我的问题是如何使用数组上的第二个元素来做同样的事情。
Instead of accessing the first element with index 0
you want to access the second element using index 1
, ie, a[1]
and b[1]
.您不想使用索引0
访问第一个元素,而是希望使用索引1
访问第二个元素,即a[1]
和b[1]
。
I suggest you should read up on how to use arrays , since this question is not specific to sorting but shows a lack of understanding arrays in general.我建议您阅读有关如何使用数组的内容,因为这个问题并非特定于排序,而是总体上表明缺乏对数组的理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.