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.

Basic sorting

Sort() sorts all the elements of a table in ascending order, keeping the original dimensions. It takes an optional reverse parameter, as shown below.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/0be035c3-cd48-4f20-8fb6-14eda073bd36/Screenshot_2021-06-08_at_12.18.34.png

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.

  1. Blank (biggest type)
  2. string (bigger than numerics)
  3. numeric types (int, double, bool (True=1,False=0)) Different types are compared using upper list

SortByCols(table, col1, col2, ..., reverse = False)

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.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/009c9abd-0276-4bf7-9009-07814fdd993b/Screenshot_2021-06-08_at_16.29.04.png

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.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/bd5ecd35-f4fe-48a5-92b8-7d245614001e/Screenshot_2021-06-08_at_16.39.44.png

SortByRows(table, row1, row2, ..., reverse = False)