如何在Java中对列表的列表进行排序?

huangapple go评论69阅读模式
英文:

How I can sort a list of list in java?

问题

我想对一个列表的列表进行排序,示例:

List<List<String>> listOflists = new ArrayList();
List<String> b = {"x", "x"};
List<String> a = {"x"};
List<String> c = {"x", "x", "x"};

listOflists.add(a);
listOflists.add(b);
listOflists.add(c);

//按数组大小排序
List<String> sortedList = listOflists.stream().sorted().collect(Collectors.toList());

我预期在forEach过程中处理listOflists:

循环 1 x
循环 2 x,x
循环 3 x,x,x

英文:

I want to sort a list of lists, example:

List&lt;List&lt;String&gt;&gt; listOflists = new ArrayList();
List&lt;String&gt; b = {&quot;x&quot;, &quot;x&quot;};
List&lt;String&gt; a = {&quot;x&quot;};
List&lt;String&gt; c = {&quot;x&quot;, &quot;x&quot;, &quot;x&quot;};

listOflists.add(a);
listOflists.add(b);
listOflists.add(c);

//Oder by size of the array
List&lt;String&gt; sortedList = listOflists.stream().sorted().collect(Collectors.toList());     

I expected that listOflists in a forEach process:

Lopp 1 x
Lopp 2 x,x
Lopp 3 x,x,x

答案1

得分: 1

[Arrays.compare](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Arrays.html#compare(T%5B%5D,T%5B%5D)) 函数用于比较两个对象数组,比较的是具有可比性的元素的字典顺序。

试一试:

    List&lt;List&lt;String&gt;&gt; listOflists = Arrays.asList(
        Arrays.asList(&quot;x&quot;, &quot;x&quot;),
        Arrays.asList(&quot;x&quot;),
        Arrays.asList(&quot;x&quot;, &quot;x&quot;, &quot;x&quot;)
    );

    List&lt;List&lt;String&gt;&gt; sortedList = listOflists.stream()
        .map(list -&gt; list.toArray(String[]::new))
        .sorted((x, y) -&gt; Arrays.compare(x, y))
        .map(array -&gt; Arrays.asList(array))
        .collect(Collectors.toList());
    System.out.println(sortedList);

输出结果:

    [[x], [x, x], [x, x, x]]

如果你想根据列表大小进行排序:

    List&lt;List&lt;String&gt;&gt; sortedList = listOflists.stream()
        .sorted(Comparator.comparing(list -&gt; list.size()))
        .collect(Collectors.toList());
英文:

Arrays.compare compares two Object arrays, within comparable elements, lexicographically.

Try this.

List&lt;List&lt;String&gt;&gt; listOflists = Arrays.asList(
    Arrays.asList(&quot;x&quot;, &quot;x&quot;),
    Arrays.asList(&quot;x&quot;),
    Arrays.asList(&quot;x&quot;, &quot;x&quot;, &quot;x&quot;)
);

List&lt;List&lt;String&gt;&gt; sortedList = listOflists.stream()
    .map(list -&gt; list.toArray(String[]::new))
    .sorted((x, y) -&gt; Arrays.compare(x, y))
    .map(array -&gt; Arrays.asList(array))
    .collect(Collectors.toList());
System.out.println(sortedList);

output:

[[x], [x, x], [x, x, x]]

If you want to sort by list size.

List&lt;List&lt;String&gt;&gt; sortedList = listOflists.stream()
    .sorted(Comparator.comparing(list -&gt; list.size()))
    .collect(Collectors.toList());

huangapple
  • 本文由 发表于 2020年9月4日 08:39:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63733383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定