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

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

How I can sort a list of list in java?

问题

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

  1. List<List<String>> listOflists = new ArrayList();
  2. List<String> b = {"x", "x"};
  3. List<String> a = {"x"};
  4. List<String> c = {"x", "x", "x"};
  5. listOflists.add(a);
  6. listOflists.add(b);
  7. listOflists.add(c);
  8. //按数组大小排序
  9. 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:

  1. List&lt;List&lt;String&gt;&gt; listOflists = new ArrayList();
  2. List&lt;String&gt; b = {&quot;x&quot;, &quot;x&quot;};
  3. List&lt;String&gt; a = {&quot;x&quot;};
  4. List&lt;String&gt; c = {&quot;x&quot;, &quot;x&quot;, &quot;x&quot;};
  5. listOflists.add(a);
  6. listOflists.add(b);
  7. listOflists.add(c);
  8. //Oder by size of the array
  9. 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

  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)) 函数用于比较两个对象数组,比较的是具有可比性的元素的字典顺序。
  2. 试一试:
  3. List&lt;List&lt;String&gt;&gt; listOflists = Arrays.asList(
  4. Arrays.asList(&quot;x&quot;, &quot;x&quot;),
  5. Arrays.asList(&quot;x&quot;),
  6. Arrays.asList(&quot;x&quot;, &quot;x&quot;, &quot;x&quot;)
  7. );
  8. List&lt;List&lt;String&gt;&gt; sortedList = listOflists.stream()
  9. .map(list -&gt; list.toArray(String[]::new))
  10. .sorted((x, y) -&gt; Arrays.compare(x, y))
  11. .map(array -&gt; Arrays.asList(array))
  12. .collect(Collectors.toList());
  13. System.out.println(sortedList);
  14. 输出结果:
  15. [[x], [x, x], [x, x, x]]
  16. 如果你想根据列表大小进行排序:
  17. List&lt;List&lt;String&gt;&gt; sortedList = listOflists.stream()
  18. .sorted(Comparator.comparing(list -&gt; list.size()))
  19. .collect(Collectors.toList());
英文:

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

Try this.

  1. List&lt;List&lt;String&gt;&gt; listOflists = Arrays.asList(
  2. Arrays.asList(&quot;x&quot;, &quot;x&quot;),
  3. Arrays.asList(&quot;x&quot;),
  4. Arrays.asList(&quot;x&quot;, &quot;x&quot;, &quot;x&quot;)
  5. );
  6. List&lt;List&lt;String&gt;&gt; sortedList = listOflists.stream()
  7. .map(list -&gt; list.toArray(String[]::new))
  8. .sorted((x, y) -&gt; Arrays.compare(x, y))
  9. .map(array -&gt; Arrays.asList(array))
  10. .collect(Collectors.toList());
  11. System.out.println(sortedList);

output:

  1. [[x], [x, x], [x, x, x]]

If you want to sort by list size.

  1. List&lt;List&lt;String&gt;&gt; sortedList = listOflists.stream()
  2. .sorted(Comparator.comparing(list -&gt; list.size()))
  3. .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:

确定