groupOrderedBy()

Convert the input stream of objects into lists of ordered objects, as measured by the given Comparator.

Implementation Notes

This is suitable for streams whose elements do not implement Comparable. For a version that uses the natural order of elements that implement Comparable, see groupOrdered(). The lists emitted from this Gatherer are unmodifiable.

Signature

groupOrderedBy(Order order, Comparator<INPUT> coparator)

  • order - A non-null Order in which to group elements. Values are Equal, Ascending, Descending, AscendingOrEqual, and DescendingOrEqual
  • comparator - A non-null Comparator to compare stream elements

Examples

Group elements the stream into lists that are strictly descending, according to the given Comparator

Stream
    .of("AAA", "AA", "A", "AA", "AA", "A")
    .gather(Gatherers4j
        .groupOrderedBy(
            Order.Descending, 
            Comparator.comparingInt(String::length)
        )
    )
    .toList();

// [ ["AAA", "AA", "A"], ["AA"], ["AA", "A"] ]

Group elements the stream into lists that are strictly ascending, according to the given Comparator

Stream
    .of("A", "AA", "AAA", "A", "AA", "AA")
    .gather(Gatherers4j
        .groupOrderedBy(
            Order.Ascending, 
            Comparator.comparingInt(String::length)
        )
    )
    .toList();

// [ ["A", "AA", "AAA"], ["A", "AA"] ["AA"] ]