sortByDesc

Sorts this DataFrame rows by the specified columns in ascending or descending (default) 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

  • sortBy that sorts rows in ascending order by default.

  • sortWith that sorts rows using a custom comparator.

Examples

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

Sorting values can also be computed inline using expr.

// Sort rows by the product of "volume" and "quantity" descending
df.sortByDesc { expr { volume * quantity } }

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>.sortByDesc(vararg columns: String): DataFrame<T>

Sorts this DataFrame rows by the specified columns in descending 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

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

  • sortBy that sorts rows in ascending order.

  • sortWith that sorts rows using a custom comparator.

Examples

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

Return

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

Parameters

columns

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


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

Sorts rows within each group of this GroupBy by the specified columns in 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 single or multiple columns using their names as Strings. (String API).

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

See also

Check out GroupBy grammar.

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

Examples

gb.sortByDesc("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 define which columns are used for sorting.

Throws

if a referenced column cannot be resolved in a group.


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

Sorts rows within each group of this GroupBy by the specified columns in ascending or descending (default) 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 descending
gb.sortByDesc { age }
// Sort group rows by "age" column values ase
// and then by the ("name"/"firstName") column values ascending
gb.sortByDesc { age and name.firstName.reversed() }
// Sort group rows by "weight" column values descending with nulls first
gb.sortByDesc { weight.nullsLast() }

Sorting values can also be computed inline using expr.

// Sort rows by the product of "volume" and "quantity" descending
gb.sortByDesc { expr { volume * quantity } }

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, C> DataFrame<T>.sortByDesc(vararg columns: KProperty<Comparable<C>?>): DataFrame<T>
fun <T> DataFrame<T>.sortByDesc(vararg columns: ColumnReference<*>): DataFrame<T>
fun <T, G> GroupBy<T, G>.sortByDesc(vararg cols: ColumnReference<*>): GroupBy<T, G>
fun <T, G> GroupBy<T, G>.sortByDesc(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