RealPivot Guide
Pivot sorting

Pivot Sort

You can sort pivots based on label values or the values of specific conditions.

Pivot Sort

There are two ways to sort in Pivot: label-based sorting and specific-condition value-based sorting. Label sorting is sorting based on the labels that exist in the column/row, and value sorting can be given conditions such as ‘sort in the order of the largest total sales quantity in the first quarter’.

Here's how to sort data in a pivot:

  1. When calling setFieldMapping() (opens in a new tab), PivotField (opens in a new tab) sortDir property (opens in a new tab) to specify default sorting (label sorting only)

  2. How to sort using the sort() (opens in a new tab) function (applicable to both label sorting and value sorting)

  3. How to use the Setup UI in Pivot

Sort by label value

The labels attribute is required when sorting by label. The labels property specifies the name and sort order (opens in a new tab) of each field.

pivot.sort({
   column: {
       method: "label",
       labels: [
         { name: "Sales Quarter", direction: "descending"},
         { name: "Sale month", direction: "descending"}
      ]
   },
   row: {
     method:"label",
     labels: [
         { name: "Brand name", direction: "ascending"},
         { name: "Car type", direction: "ascending"}
      ]
   }
});

Sort by value

When sorting by value, specify the value field name (fieldName), sort order (opens in a new tab), and sort conditions. The conditions property must specify the row field for column and the column field for row as the condition to be sorted. If conditions is omitted, sort by overall summary.

pivot.sort({
   column: {
       method: "value",
       fieldName: "Sales Quantity",
       direction: "descending",
   },
   row: {
       method: "value",
       fieldName: "Sales Quantity",
       direction: "descending",
       conditions: [{
       name: "Year of sale", value: 2016
       }]
   }
});

Complex Sort

In a pivot, you can specify separate sorting criteria for columns and rows.

pivot.sort({
   column: {
       method: "label",
       labels: [
         { name: "Sales Quarter", direction: "descending"},
         { name: "Sale month", direction: "descending"}
      ]
   },
   row: {
       method: "value",
       fieldName: "Sales Quantity",
       direction: "descending",
       conditions: [{
       name: "Year of sale", value: 2016
       }]
   }
});