如何宣布Java比较器类在上升和降序中按第二个元素对数组进行排序
int arr[][] = new int[][] {
{3, 7},
{1, 2},
{8, 9},
{4, 2},
{5, 3},
{0,90}
};
这是我的数组,我可以根据第一个元素对其进行排序或下降顺序 Bellow代码:
Arrays.sort(arr,(a,b)->Integer.compare(a[0], b[0]));
现在我的问题是如何使用数组上的第二个元素来做同样的事情。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是使用索引
0
访问第一个元素,您需要使用索引1
访问第二个元素,即> B [1] 。我建议您应该在如何使用阵列,,由于这个问题不是针对排序的特定的,而是表明一般缺乏了解数组。
Instead of accessing the first element with index
0
you want to access the second element using index1
, i.e.,a[1]
andb[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.