如何宣布Java比较器类在上升和降序中按第二个元素对数组进行排序

发布于 2025-02-10 17:38:45 字数 345 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一笔一画续写前缘 2025-02-17 17:38:45

而不是使用索引0访问第一个元素,您需要使用索引1访问第二个元素,即> B [1] 。

我建议您应该在如何使用阵列,,由于这个问题不是针对排序的特定的,而是表明一般缺乏了解数组。

Instead of accessing the first element with index 0 you want to access the second element using index 1, i.e., a[1] and 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文