sortByGroup

fun <T, G, C> GroupBy<T, G>.sortByGroup(nullsLast: Boolean = false, expression: DataFrameExpression<G, C>): GroupBy<T, G>

Sorts the key-group pairs of this GroupBy by a value computed for each group using the specified expression ascending.

DataFrameExpression is a lambda that receives each group as a DataFrame, both as the receiver (this) and as the lambda argument (it), and returns the value used to order the groups.

Returns a new GroupBy containing the same keys and groups in the resulting order. The contents of the groups remain unchanged.

Don't confuse this with sortBy that sorts rows within groups.

See also

Check out GroupBy grammar.

For more information: See "GroupBy Transformation" on the documentation website.

Example

// Sorts groups by the product of their size and the maximum value of the "quantity" column ascending
gb.sortByGroup { group -> group.rowsCount() * group.quantity.max() }

Return

A new GroupBy with its key-group pairs sorted by the values produced by expression.

Parameters

nullsLast

Whether null values are considered as the largest values when sorting. Default is false.

expression

The expression used to compute the sorting value for each group.