[]
        
(Showing Draft Content)

Sort

The Sort property ('S') defines the sorting type within the template. This property applies to a single or multiple columns, determined by the respective cell values. The sorting functionality extends beyond sorting in ascending and descending order; it allows the application of custom sorting rules to single or multiple columns.

DsExcel implements sorting by observing the following basic rules:

  1. DsExcel processes the sort function of a template by distinguishing the cells into template cells and instance cells. Template cells are the cells in the template file, while instance cells are the cells that are created after the template has been processed.

  2. DsExcel treats the relationship between the template cells as a parent-child relationship. A similar relationship applies between the instance cells.

  3. DsExcel sorts the cells between brother instance cells but cannot change the parent-child relationship. Hence, the instance cells to be sorted must fulfill the following two conditions:

    1. They come from the same template cell.

    2. They have a common parent cell.

DsExcel sort context diagram showing Worksheet, A1, A2, B1, B2, C1, and C2 parent-child and brother relationships.


For example, let's consider the following data source:

DsExcel source worksheet listing Area, City, Product, and Revenue records used to demonstrate the template Sort property.

If you use the following template, the cells in column D cannot be sorted because the Sort property is restrained by context cell A1 (i.e., no common parent cell).

DsExcel template with S=desc on the Revenue field, illustrating invalid sorting when the field lacks a common parent context.

Hence, you can only add the Sort property to cell A1. Then the template will be as follows:

DsExcel corrected template applying G=L and S=(D1 desc) to the Area field for context-aware revenue sorting.

Result:

DsExcel generated worksheet showing Area, City, Product, and Revenue rows sorted by descending Revenue through the Sort property.

You can only sort cells that have the Sort property; cells without the Sort property will not be sorted. This means that in the above data source, sorting the value of A1 based on the value of D1 will only sort A1, not D1.

Sorting can be performed in three ways:

  • Enum

  • Array

  • Expression

Value: Enum

S=Asc (default value): Ascending

S=Desc : Descending

S=None: None

Value: Array

S={“X“, “Y“,”Z”}

Value: Expression

S=(Cell A Asc {“X“, “Y“,”Z”}, Cell B Desc)

Example 1: Sorting Single Column

{{ds.field(S=Desc)}}

The below image shows how the template fields are expanded based on their sorting type. You can also download the Excel template layout used in below example.

DsExcel template and generated report showing a single column sorted in descending order with the S=Desc property.

Example 2: Sorting Multiple Columns

{{ds.OrderID(S=(C12,D12 desc),G=List)}}

The below image shows how the Order ID column is sorted based on C12 and D12 cells. You can also download the Excel template layout used in below example.

DsExcel template and generated order report showing OrderID sorted by C12 and D12 descending with the S property.


Example 3: Sorting using Custom Rule

{{ds.City(S=(A12 desc {"New York", "Chicago", "Minnesota", "Santiago", "Fremont", "Quito", "Medillin", "Buenos Aires"}))}}

The below image shows how the City column is sorted based on custom sorting rule. You can also download the Excel template layout used in below example.

DsExcel custom sort template and generated city sales report using S=(A12 desc {...}) to apply a specified city order.