sort
The sort
method returns a new PartitionedTable
in which the rows are ordered based on values in a specified set of columns.
danger
A partitioned table cannot be sorted on its __CONSTITUENT__
column.
Syntax
sort(
order_by: Union[str, Sequence[str]],
order: Union[SortDirection, Sequence[SortDirection]] = None
) -> PartitionedTable
Parameters
Parameter | Type | Description |
---|---|---|
order_by | Union[str, Sequence[str]] | The column(s) to be sorted on. Cannot include the |
order | Union[SortDirection, Sequence[SortDirection]] | The sort order(s). Default is |
Returns
A sorted PartitionedTable
.
Examples
The following example partitions a source table on a single column. The partitioned table is then sorted on that key column in descending order.
from deephaven import empty_table
from deephaven import SortDirection
source = empty_table(25).update(["IntCol = randomInt(1, 5)", "StrCol = `value`"])
partitioned_table = source.partition_by(["IntCol"])
result_unsorted = partitioned_table.table
result_sorted = partitioned_table.sort(
order_by="IntCol", order=SortDirection.DESCENDING
).table
- result_sorted
- result_unsorted
- source
The following example partitions a source table on two columns. The partitioned table is then sorted in ascending order on Exchange
and descending order on Coin
.
from deephaven.column import double_col, string_col
from deephaven import SortDirection
from deephaven import new_table
exchanges = ["Kraken", "Coinbase", "Coinbase", "Kraken", "Kraken", "Kraken", "Coinbase"]
coins = ["BTC", "ETH", "DOGE", "ETH", "DOGE", "BTC", "BTC"]
prices = [30100.5, 1741.91, 0.068, 1739.82, 0.065, 30097.96, 30064.25]
source = new_table(
cols=[
string_col(name="Exchange", data=exchanges),
string_col(name="Coin", data=coins),
double_col(name="Price", data=prices),
]
)
partitioned_table = source.partition_by(by=["Exchange", "Coin"])
result_unsorted = partitioned_table.table
result_sorted = partitioned_table.sort(
order_by=["Exchange", "Coin"],
order=[SortDirection.ASCENDING, SortDirection.DESCENDING],
).table
- result_sorted
- result_unsorted
- source