0

I want to sort an ArrayList of ArrayList. How can I use custom comparator in Collections.sort(); ??? my IDE shows error

ArrayList<ArrayList<Integer>> A

Collections.sort(A,new Comparator<ArrayList<Integer>>(){ }); the above code is not working

Shivansh Narayan
  • 506
  • 6
  • 15

1 Answers1

1

If you want a single sorted list you can try this :

    ArrayList<ArrayList<Integer>> myListOfList = new ArrayList<>();
    List<Integer> sortedList = myListOfList.stream()
            .flatMap(List::stream)
            .sorted(Integer::compareTo)
            .collect(Collectors.toList());

or this :

    List<Integer> sortedList = myListOfList.stream()
            .flatMap(List::stream)
            .sorted(Comparator.naturalOrder())
            .collect(Collectors.toList());
brouille
  • 295
  • 4
  • 14