Planarly provides functions for sorting entire tables. The basic Sort()
treats the table as a homogenous set of values and sorts them in ascending or descending order. SortByRows()
and SortByCols()
allow you to sort tables in a similar manner to what classic spreadsheets provide. These are essential when your table may represent entities with attributes grouped in rows or columns. They allow you to specify the row(s) or column(s) to sort by and keep all the adjacent values in sync.
Sort()
sorts all the elements of a table in ascending order, keeping the original dimensions. It takes an optional reverse
parameter, as shown below.
Note that the final result is in row-major order. If you want the results in column-major order simply wrap the call to Sort()
in a call to tranpose, T()
: T(Sort(#a))
.
If the table being sorted contains different types, then the result will reflect how Planarly interprets the relative size of the different types. Below is the list of types from biggest to smallest. This is so with a normal ascending sort Blank
values are sorted to the end of the table.
This function performs the classic spreadsheet table sort. In this arrangement the data in your table represents entities, with each column holding a different attribute. All the attributes for a single entity are on a single row.
The parameters to SortByCols()
is a table, followed by the columns in the order you want the sort to be performed, followed by an optional reverse
flag. The default is to sort in ascending order.
SortByCols()
returns a copy of the table with the rows rearranged so that the elements in column col1
are sorted. If col2
is specified, any rows with equal values in col1
are then sorted by col2
values. And so on if there are more columns provided.
Below you can see this in action. We have a table #a
and labels have been used to specify each of the columns. The first example sorts only by the column #sku
. The second by sku
and then by weight
, reversing the order of the first two items compared to the single column sort.
You can also use Planarly's square bracket notation to specify columns. It is similar to array access in some languages. The example above re-written with square bracket notation is below.