Skip to content

Commit 303c920

Browse files
committed
docs and refactorings
1 parent b34a541 commit 303c920

File tree

2 files changed

+23
-7
lines changed
  • core/src/main/kotlin/org/jetbrains/kotlinx/dataframe

2 files changed

+23
-7
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/sort.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ public interface SortDsl<out T> : ColumnsSelectionDsl<T> {
3333
public fun <C> KProperty<C?>.nullsLast(flag: Boolean = true): ColumnSet<C?> = toColumnAccessor().nullsLast(flag)
3434
}
3535

36+
/**
37+
* [SortColumnsSelector] is used to express or select multiple columns to sort by, represented by [ColumnSet]`<C>`,
38+
* using the context of [SortDsl]`<T>` as `this` and `it`.
39+
*
40+
* So:
41+
* ```kotlin
42+
* SortDsl<T>.(it: SortDsl<T>) -> ColumnSet<C>
43+
* ```
44+
*/
3645
public typealias SortColumnsSelector<T, C> = Selector<SortDsl<T>, ColumnSet<C>>
3746

3847
// region DataColumn

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/sort.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,23 @@ import org.jetbrains.kotlinx.dataframe.impl.columns.toColumns
2121
import org.jetbrains.kotlinx.dataframe.kind
2222
import org.jetbrains.kotlinx.dataframe.nrow
2323

24-
internal fun <T, G> GroupBy<T, G>.sortByImpl(columns: SortColumnsSelector<G, *>): GroupBy<T, G> {
25-
return toDataFrame()
24+
@Suppress("UNCHECKED_CAST", "RemoveExplicitTypeArguments")
25+
internal fun <T, G> GroupBy<T, G>.sortByImpl(columns: SortColumnsSelector<G, *>): GroupBy<T, G> =
26+
toDataFrame()
27+
28+
// sort the individual groups by the columns specified
2629
.update { groups }
2730
.with { it.sortByImpl(UnresolvedColumnsPolicy.Skip, columns) }
31+
32+
// sort the groups by the columns specified (must be either be the keys column or "groups")
33+
// will do nothing if the columns specified are not the keys column or "groups"
2834
.sortByImpl(UnresolvedColumnsPolicy.Skip, columns as SortColumnsSelector<T, *>)
29-
.asGroupBy { it.getFrameColumn(groups.name()).castFrameColumn() }
30-
}
35+
36+
.asGroupBy { it.getFrameColumn(groups.name()).castFrameColumn<G>() }
3137

3238
internal fun <T, C> DataFrame<T>.sortByImpl(
3339
unresolvedColumnsPolicy: UnresolvedColumnsPolicy = UnresolvedColumnsPolicy.Fail,
34-
columns: SortColumnsSelector<T, C>
40+
columns: SortColumnsSelector<T, C>,
3541
): DataFrame<T> {
3642
val sortColumns = getSortColumns(columns, unresolvedColumnsPolicy)
3743
if (sortColumns.isEmpty()) return this
@@ -71,7 +77,6 @@ internal fun <T, C> DataFrame<T>.getSortColumns(
7177
else -> throw IllegalStateException("Can not use ${col.kind} as sort column")
7278
}
7379
}
74-
}
7580

7681
internal enum class SortFlag { Reversed, NullsLast }
7782

@@ -86,12 +91,14 @@ internal fun <C> ColumnWithPath<C>.addFlag(flag: SortFlag): ColumnWithPath<C> {
8691
SortFlag.NullsLast -> SortColumnDescriptor(col.column, col.direction, true)
8792
}
8893
}
94+
8995
is ValueColumn -> {
9096
when (flag) {
9197
SortFlag.Reversed -> SortColumnDescriptor(col, SortDirection.Desc)
9298
SortFlag.NullsLast -> SortColumnDescriptor(col, SortDirection.Asc, true)
9399
}
94100
}
101+
95102
else -> throw IllegalArgumentException("Can not apply sort flag to column kind ${col.kind}")
96103
}.addPath(path)
97104
}
@@ -103,7 +110,7 @@ internal class ColumnsWithSortFlag<C>(val column: ColumnSet<C>, val flag: SortFl
103110
internal class SortColumnDescriptor<C>(
104111
val column: ValueColumn<C>,
105112
val direction: SortDirection = SortDirection.Asc,
106-
val nullsLast: Boolean = false
113+
val nullsLast: Boolean = false,
107114
) : ValueColumn<C> by column
108115

109116
internal enum class SortDirection { Asc, Desc }

0 commit comments

Comments
 (0)