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 also
sortByDescthat sorts rows in descending order by default.sortWiththat 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
The Sort Columns Selector that defines which columns are used for sorting, in which order and direction.
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
overload that usessortBy { }SortDslfor selecting columns, allowing specifying sort directions andnulls position.sortByDescthat sorts rows in descending order.sortWiththat 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
The Column Names that defines which columns are used for sorting.
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
overload that usessortBy { }SortDslfor selecting columns, allowing specifying sort directions andnulls position.sortByDescthat sorts rows in descending order.sortByGroupandsortByGroupDescthat sort key-group pairs by values computed for each group.sortByCountandsortByCountAscthat sort key-group pairs by group size.sortByKeyandsortByKeyDescthat sort key-group pairs by key values.
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
The Column Names that defines which columns are used for sorting.
Throws
if a referenced column cannot be resolved in a group.
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 also
sortByDescthat sorts rows in descending order by default.sortByGroupandsortByGroupDescthat sort key-group pairs by values computed for each group.sortByCountandsortByCountAscthat sort key-group pairs by group size.sortByKeyandsortByKeyDescthat sort key-group pairs by key values.
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
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.
Deprecated
It's recommended to migrate to the String or Extension Properties Access API https://kotlin.github.io/dataframe/apilevels.html