
# 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()](http://help.realgrid.com/pivotApi/RealPivot/setFieldMapping/), [PivotField](http://help.realgrid.com/pivotApi/types/PivotField/) [sortDir property](http://help.realgrid.com/pivotApi/types/SortDirection/) to specify default sorting (label sorting only)

2. How to sort using the [sort()](http://help.realgrid.com/pivotApi/RealPivot/sort/) 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](http://help.realgrid.com/pivotApi/types/SortDirection/) of each field.


<GridView demo='pivotguides/03-pivotSorting/01' height={700}/>


```js
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](http://help.realgrid.com/pivotApi/types/SortDirection/), 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.

<GridView demo='pivotguides/03-pivotSorting/02' height={700}/>

```js
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.

<GridView demo='pivotguides/03-pivotSorting/03' height={700}/>

```js
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
       }]
   }
});
```