Sorting tables with finaquant® protos

All the C# code examples presented on this page can be found in the related demo function of MS Visual Studio project FinaquantProtosStarter which can be downloaded at the product page of the free .NET library finaquant® protos.

Sorting rows of tables after some fields may be required for certain table calculations. The table function SortRows()1 in finaquant® protos (free .net library) can be used for sorting tables:

// Sort table after given fields and sort options (ASC/DESC).
// Sort after all fields in order (ASC) if SortStr is null or empty string ""
// Tbl: Input table
// SortStr: Comma separated field names and sort option, like "field1 ASC, field2 DESC"
// Returns: Sorted table
public static MatrixTable SortRows(MatrixTable Tbl, string SortStr = null)

You may find some examples with the CostTable below.

Shuffle rows of CostTable before sorting

// shuffle rows
CostTable = MatrixTable.ShuffleRows(CostTable, 5000);
// view table
MatrixTable.View_MatrixTable(CostTable, "Shuffled CostTable");

Shuffled CostTable

1) Sort CostTable after fields category, product (descending order) and date

SortStr = "category, product DESC, date";
MatrixTable SortedTbl = MatrixTable.SortRows(CostTable, SortStr);
// view table
MatrixTable.View_MatrixTable(SortedTbl, "Sorted CostTable, SortStr = \"category, product DESC, date\"");

Sorted CostTable 1

2) Sort CostTable after all fields (from category to costs) in ascending order

SortedTbl = MatrixTable.SortRows(CostTable);
MatrixTable.View_MatrixTable(SortedTbl, "Sorted CostTable, SortStr = null");

Sorted CostTable 2

3) Sort CostTable after fields date, brand (descending order) and costs

SortStr = "date, brand DESC, costs";
SortedTbl = MatrixTable.SortRows(CostTable, SortStr);
MatrixTable.View_MatrixTable(SortedTbl, "Sorted CostTable, SortStr = \"date, brand DESC, costs\"");

Sorted CostTable 3

Copyright secured by Digiprove © 2013 Tunc Ali Kütükcüoglu
  1. This table function is available in releases 1.02 and higher []
This entry was posted in Calculation engine and tagged , . Bookmark the permalink.

Leave a Reply