Skip to content

Order By

Sort a result with .order_by([cols], desc) on the Select builder. The first argument is the list of columns to sort by; the second is a booltrue for descending, false for ascending.

Assume a live runtime and the trades table

use rayforce::{col, Runtime, Table, Value};
let _rt = Runtime::new()?;
let t = trades(); // sym / price / size — see the Overview

Descending sort

let r = t
    .select()
    .col("price")
    .order_by(["price"], true) // descending
    .execute()?;
println!("{r}");
┌─────────────────────────────────────┐
│                price                │
│                 F64                 │
├─────────────────────────────────────┤
│ 300.0                               │
│ 210.0                               │
│ 200.0                               │
│ 110.0                               │
│ 100.0                               │
├─────────────────────────────────────┤
│ 5 rows (5 shown) 1 columns (1 shown)│
└─────────────────────────────────────┘

Ascending sort

Pass false for ascending order:

let r = t.select().col("price").order_by(["price"], false).execute()?;

Multiple sort columns

Provide more than one column to sort by the first, breaking ties with the next:

let r = t
    .select()
    .cols(["sym", "price"])
    .order_by(["sym", "price"], false)
    .execute()?;

Chaining with other operations

.order_by composes with filtering, grouping, and projection. It applies to the final result:

let r = t
    .select()
    .cols(["sym", "price"])
    .filter(col("price").gt(150.0))
    .order_by(["price"], true)
    .execute()?;

Continue with Update to modify table columns.