sortBy

Sorts this DataFrame rows by the specified columns in ascending (default) or descending order.

Returns a new DataFrame containing the same rows, sorted according to the selected columns.

Select columns to sort by, adjust sorting order and nulls position using SortDsl.

The Sort DSL allows selecting columns to sort rows by values in these columns. It also allows reversing the sort order for individual columns or column sets, and controlling the position of null values.

The order in which columns are selected determines the sort priority.

By default, all selected columns are sorted in the original order (ascending in sortBy and descending in sortByDesc). Use reversed to impose the reverse ordering for a column or column set.

By default, null values are considered the smallest values when sorting. Use nullsLast to treat them as the largest values.

See Columns Selection DSL.

See also

  • sortByDesc that sorts rows in descending order by default.

  • sortWith that sorts rows using a custom comparator.

Examples

// Sort rows by "age" column values ascending
df.sortBy { age }
// Sort rows by "age" column values descending
// and then by the ("name"/"firstName") column values ascending
df.sortBy { age.reversed() and name.firstName }
// Sort rows by "weight" column values ascending with nulls last
df.sortBy { weight.nullsLast() }

Sorting values can also be computed inline using expr.

// Sort rows by the product of "volume" and "quantity" descending
df.sortBy { expr { volume * quantity }.reversed() }

Return

A new DataFrame with the original rows sorted based on the specified columns and directions.

Parameters

columns

The Sort Columns Selector that defines which columns are used for sorting, in which order and direction.


fun <T> DataFrame<T>.sortBy(vararg cols: String): DataFrame<T>

Sorts this DataFrame rows by the specified columns in ascending order.

Returns a new DataFrame containing the same rows, sorted according to the selected columns.

Select single or multiple columns using their names as Strings. (String API).

The order in which columns are selected determines the sort priority.

See also

  • sortBy { } overload that uses SortDsl for selecting columns, allowing specifying sort directions and nulls position.

  • sortByDesc that sorts rows in descending order.

  • sortWith that sorts rows using a custom comparator.

Examples

df.sortBy("age")
// Sort rows by "age" column values ascending
// and then by the ("name") column values ascending
df.sortBy("age", "name")

Return

A new DataFrame with the original rows sorted based on the specified columns.

Parameters

cols

The Column Names that defines which columns are used for sorting.


fun <T, G> GroupBy<T, G>.sortBy(vararg cols: String): GroupBy<T, G>

Sorts rows within each group of this GroupBy by the specified columns in ascending order.

Returns a new GroupBy containing the same keys and groups, with rows sorted within each group according to the selected columns.

Select single or multiple columns using their names as Strings. (String API).

The order in which columns are selected determines the sort priority.

Don't confuse this with sortByGroup, sortByCount, and sortByKey, which sort key-group pairs instead.

See also

Check out GroupBy grammar.

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

Examples

gb.sortBy("age")
// Sort group rows by "age" column values descending
// and then by the ("name") column values descending
gb.sortByDesc("age", "name")

Return

A new GroupBy with rows sorted within each group and the original key-group order preserved.

Parameters

cols

The Column Names that defines which columns are used for sorting.

Throws

if a referenced column cannot be resolved in a group.


fun <T, G, C> GroupBy<T, G>.sortBy(selector: SortColumnsSelector<G, C>): GroupBy<T, G>

Sorts rows within each group of this GroupBy by the specified columns in ascending (default) or descending order.

Returns a new GroupBy containing the same keys and groups, with rows sorted within each group according to the selected columns.

Don't confuse this with sortByGroup, sortByCount, and sortByKey, which sort key-group pairs instead.

Select columns to sort by, adjust sorting order and nulls position using SortDsl.

The Sort DSL allows selecting columns to sort rows by values in these columns. It also allows reversing the sort order for individual columns or column sets, and controlling the position of null values.

The order in which columns are selected determines the sort priority.

By default, all selected columns are sorted in the original order (ascending in sortBy and descending in sortByDesc). Use reversed to impose the reverse ordering for a column or column set.

By default, null values are considered the smallest values when sorting. Use nullsLast to treat them as the largest values.

See Columns Selection DSL.

See also

Check out GroupBy grammar.

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

Examples

// Sort group rows by "age" column values ascending
gb.sortBy { age }
// Sort group rows by "age" column values descending
// and then by the ("name"/"firstName") column values ascending
gb.sortBy { age.reversed() and name.firstName }
// Sort group rows by "weight" column values ascending with nulls last
gb.sortBy { weight.nullsLast() }

Sorting values can also be computed inline using expr.

// Sort rows by the product of "volume" and "quantity" descending
gb.sortBy { expr { volume * quantity }.reversed() }

Return

A new GroupBy with rows sorted within each group and the original key-group order preserved.

Parameters

selector

The Sort Columns Selector that defines which columns are used for sorting within each group, in which order and direction.

Throws

if a referenced column cannot be resolved in a group.


fun <T> DataFrame<T>.sortBy(vararg cols: ColumnReference<*>): DataFrame<T>
fun <T> DataFrame<T>.sortBy(vararg cols: KProperty<Comparable<*>?>): DataFrame<T>
fun <T, G> GroupBy<T, G>.sortBy(vararg cols: ColumnReference<*>): GroupBy<T, G>
fun <T, G> GroupBy<T, G>.sortBy(vararg cols: KProperty<Comparable<*>?>): GroupBy<T, G>

Deprecated

It's recommended to migrate to the String or Extension Properties Access API https://kotlin.github.io/dataframe/apilevels.html