Order Operations¶
RayforceDB provides functions for sorting and ordering Vectors, Lists, and Tables.
Sorting Functions¶
Asc¶
Sorts elements in ascending order. Returns a new sorted Vector or List.
(asc [3 1 4 1 5 9 2 6])
[1 1 2 3 4 5 6 9]
(asc [150.25 300.50 125.75 200.00])
[125.75 150.25 200.00 300.50]
(asc (list "banana" "apple" "cherry"))
(apple banana cherry)
Desc¶
Sorts elements in descending order. Returns a new sorted Vector or List.
(desc [3 1 4 1 5 9 2 6])
[9 6 5 4 3 2 1 1]
(desc [150.25 300.50 125.75 200.00])
[300.50 200.00 150.25 125.75]
(desc (list "banana" "apple" "cherry"))
(cherry banana apple)
Index Functions¶
Iasc¶
Returns indices that would sort the input in ascending order.
(iasc [3 1 4 1 5])
[1 3 0 2 4]
(set prices [150.25 300.50 125.75])
(set symbols ['AAPL 'MSFT 'GOOG])
(at symbols (iasc prices))
[GOOG AAPL MSFT]
(iasc (list "banana" "apple" "cherry"))
[1 0 2]
Idesc¶
Returns indices that would sort the input in descending order.
(idesc [3 1 4 1 5])
[4 2 0 1 3]
;; Use indices to reorder another vector
(set prices [150.25 300.50 125.75])
(set symbols ['AAPL 'MSFT 'GOOG])
(at symbols (idesc prices))
[MSFT AAPL GOOG]
(idesc (list "banana" "apple" "cherry"))
[2 0 1]
Ranking Functions¶
Rank¶
Returns the rank (position in sorted order) of each element in a Vector. The smallest element gets rank 0, the next smallest gets rank 1, and so on.
(rank [30 10 20])
[2 0 1]
(rank [5 3 1 4 2])
[4 2 0 3 1]
;; Rank prices: smallest price gets rank 0
(rank [150.25 300.50 125.75])
[1 2 0]
Xrank¶
Assigns each element to a bucket based on its rank, dividing the data into n equal-sized groups. Returns bucket indices from 0 to n-1.
(xrank [30 10 20 40 50 60] 3)
[1 0 0 1 2 2]
(xrank [1 2 3 4] 2)
[0 0 1 1]
;; Divide prices into 3 buckets (tertiles)
(xrank [150.25 300.50 125.75 200.00 175.50] 3)
[1 2 0 2 1]
Table Sorting Functions¶
Xasc¶
Sorts a Table in ascending order based on specified columns. When multiple columns are specified, sorting is done by the first column, then by the second column for ties, and so on.
(set trades (table [symbol price quantity]
(list ['AAPL 'MSFT 'GOOG] [150.25 300.50 125.75] [100 200 150])))
(xasc [price] trades)
┌────────┬────────┬──────────┐
│ symbol │ price │ quantity │
├────────┼────────┼──────────┤
│ GOOG │ 125.75 │ 150 │
│ AAPL │ 150.25 │ 100 │
│ MSFT │ 300.50 │ 200 │
└────────┴────────┴──────────┘
;; Sort by multiple columns: first by symbol, then by price
(xasc [symbol price] trades)
The xasc function takes two arguments:
1. A Vector of Symbols representing column names to sort by
2. The Table to sort
Xdesc¶
Sorts a Table in descending order based on specified columns. When multiple columns are specified, sorting is done by the first column, then by the second column for ties, and so on.
(set trades (table [symbol price quantity]
(list ['AAPL 'MSFT 'GOOG] [150.25 300.50 125.75] [100 200 150])))
(xdesc [price] trades)
┌────────┬────────┬──────────┐
│ symbol │ price │ quantity │
├────────┼────────┼──────────┤
│ MSFT │ 300.50 │ 200 │
│ AAPL │ 150.25 │ 100 │
│ GOOG │ 125.75 │ 150 │
└────────┴────────┴──────────┘
;; Sort by multiple columns: first by price (descending), then by quantity (descending)
(xdesc [price quantity] trades)
The xdesc function takes two arguments:
1. A Vector of Symbols representing column names to sort by
2. The Table to sort
Numeric Operations¶
Neg¶
Returns the negative of a number or applies negation element-wise to Vectors.