Sorts a table by the specified keys. Args: table : pw.Table The table to be sorted. key (ColumnExpression[int | float | datetime | str | bytes]): An expression to sort by. instance : ColumnReference or None
(
self,
key: expr.ColumnExpression,
instance: expr.ColumnExpression | None = None,
)
| 2407 | @contextualized_operator |
| 2408 | @check_arg_types |
| 2409 | def sort( |
| 2410 | self, |
| 2411 | key: expr.ColumnExpression, |
| 2412 | instance: expr.ColumnExpression | None = None, |
| 2413 | ) -> Table: |
| 2414 | """ |
| 2415 | Sorts a table by the specified keys. |
| 2416 | |
| 2417 | Args: |
| 2418 | table : pw.Table |
| 2419 | The table to be sorted. |
| 2420 | key (ColumnExpression[int | float | datetime | str | bytes]): |
| 2421 | An expression to sort by. |
| 2422 | instance : ColumnReference or None |
| 2423 | An expression with instance. Rows are sorted within an instance. |
| 2424 | ``prev`` and ``next`` columns will only point to rows that have the same instance. |
| 2425 | |
| 2426 | Returns: |
| 2427 | pw.Table: The sorted table. Contains two columns: ``prev`` and ``next``, containing the pointers |
| 2428 | to the previous and next rows. |
| 2429 | |
| 2430 | Example: |
| 2431 | |
| 2432 | >>> import pathway as pw |
| 2433 | >>> table = pw.debug.table_from_markdown(''' |
| 2434 | ... name | age | score |
| 2435 | ... Alice | 25 | 80 |
| 2436 | ... Bob | 20 | 90 |
| 2437 | ... Charlie | 30 | 80 |
| 2438 | ... ''') |
| 2439 | >>> table = table.with_id_from(pw.this.name) |
| 2440 | >>> table += table.sort(key=pw.this.age) |
| 2441 | >>> pw.debug.compute_and_print(table, include_id=True) |
| 2442 | | name | age | score | prev | next |
| 2443 | ^GBSDEEW... | Alice | 25 | 80 | ^EDPSSB1... | ^DS9AT95... |
| 2444 | ^EDPSSB1... | Bob | 20 | 90 | | ^GBSDEEW... |
| 2445 | ^DS9AT95... | Charlie | 30 | 80 | ^GBSDEEW... | |
| 2446 | >>> table = pw.debug.table_from_markdown(''' |
| 2447 | ... name | age | score |
| 2448 | ... Alice | 25 | 80 |
| 2449 | ... Bob | 20 | 90 |
| 2450 | ... Charlie | 30 | 80 |
| 2451 | ... David | 35 | 90 |
| 2452 | ... Eve | 15 | 80 |
| 2453 | ... ''') |
| 2454 | >>> table = table.with_id_from(pw.this.name) |
| 2455 | >>> table += table.sort(key=pw.this.age, instance=pw.this.score) |
| 2456 | >>> pw.debug.compute_and_print(table, include_id=True) |
| 2457 | | name | age | score | prev | next |
| 2458 | ^GBSDEEW... | Alice | 25 | 80 | ^T0B95XH... | ^DS9AT95... |
| 2459 | ^EDPSSB1... | Bob | 20 | 90 | | ^RT0AZWX... |
| 2460 | ^DS9AT95... | Charlie | 30 | 80 | ^GBSDEEW... | |
| 2461 | ^RT0AZWX... | David | 35 | 90 | ^EDPSSB1... | |
| 2462 | ^T0B95XH... | Eve | 15 | 80 | | ^GBSDEEW... |
| 2463 | """ |
| 2464 | instance = clmn.ColumnExpression._wrap(instance) |
| 2465 | self._check_for_disallowed_types(key, instance) |
| 2466 | context = clmn.SortingContext( |