英文:
Sort List<List<Integer>> based on the Integer in the second index of the inner List<Integer>?
问题
Sure, here's the translation:
假设我有以下输入:
[[5,3,6,7],[2,1,6,3],[3,2,6,3],[5,4,4,3]]
如何根据每个列表的第二个数字进行排序,使得我得到以下结果:(因为 1 < 2 < 3 < 4)
[[2,1,6,3],[3,2,6,3],[5,3,6,7],[5,4,4,3]]
我想你可以使用流(Streams)做一些操作,但我还没完全弄明白。
英文:
Imagine I have the following input:
[[5,3,6,7],[2,1,6,3],[3,2,6,3],[5,4,4,3]]
How could I sort it based on the second number of each list, so that I have the following: (because 1 < 2 < 3 < 4)
[[2,1,6,3],[3,2,6,3],[5,3,6,7],[5,4,4,3]]
I imagine you can do something with Streams, but I haven't quite figured it out.
答案1
得分: 3
好的,以下是您要求的翻译内容:
这其实非常简单。您可以查看列表排序和自定义比较器。
public static void sortBySecondIndex(List<List<Integer>> data) {
data.sort(Comparator.comparingInt(one -> one.get(1)));
}
英文:
Well, this is very simple. Actually you can look at list sorting and custom comparator.
public static void sortBySecondIndex(List<List<Integer>> data) {
data.sort(Comparator.comparingInt(one -> one.get(1)));
}
答案2
得分: 1
list.sort((a, b) -> a.get(1).compareTo(b.get(1)));
英文:
list.sort((a, b) -> a.get(1).compareTo(b.get(1)));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论