假设我有以下输入:
[[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做点什么,但我还没有完全弄清楚。
这很简单。实际上,您可以查看列表排序和自定义比较器。
public static void sortBySecondIndex(List<List<Integer>> data) {
data.sort(Comparator.comparingInt(one -> one.get(1)));
}
list.sort((a, b) -> a.get(1).compareTo(b.get(1)));