# Sort

## Content

SpreadJS lets you sort pivot table data so that you can organize it for quick and convenient analysis. Sorting is especially useful when dealing with large amounts of data. You can sort data by using `PivotTable.sort` function that accepts the object of `sortInfo` as a parameter.
SpreadJS allows users to sort and order PivotTable data in four different ways:

1. Sort by field item name
2. Sort by value
3. Sort by custom field item value
4. Sort by custom callback

## Sort by Field Item Name

To sort pivot table data by a field, you can specify the field name and use the `sortType` property of `sortInfo` and set the sorting order to ascending, descending, or custom.

The below image demonstrates descending sort order applied to the "Category" field.
![pivot-sort-by-field-name](https://gcdocumentsitekrblob.blob.core.windows.net/document-site-files/images/df1fe1ee-eb3c-4da7-8c20-a0d8d2b7e734/pivot-sort-by-field-name.6635c7.png)

The following code sample shows how to sort pivot table data by field item name:

```javascript
let pivotTable = spread.getActiveSheet().pivotTables.get("PivotTable");
 var sortInfo = {
  sortType: GC.Pivot.SortType.desc
 };
pivotTable.sort("Category", sortInfo);
```

### Using Designer

You can also sort data through the designer by clicking **More Sort Options..** from the **Filter Dropdown** of a field. This option opens a **Sort Field** dialog where you can select the target field and set the sort type as ascending or descending.

The following image demonstrates how to sort by field item name using the designer:
![pivot-sort-by-field-name](https://gcdocumentsitekrblob.blob.core.windows.net/document-site-files/images/df1fe1ee-eb3c-4da7-8c20-a0d8d2b7e734/pivot-sort-by-field-name.6dac76.gif)

## Sort by Value

To sort a pivot table data by values of a field, you can set the `sortType` and `sortValueFieldName` properties of the `sortInfo` class. The sortValueFieldName property lets you specify name of the field by which you want to sort the data. This feature lets you sort the pivot table data by the values in the "Grand Total" column.

The below image demonstrates the **"Category"** field being sorted in ascending order by the "**Quantity**" value.
![pivot-sort-by-value](https://gcdocumentsitekrblob.blob.core.windows.net/document-site-files/images/df1fe1ee-eb3c-4da7-8c20-a0d8d2b7e734/pivot-sort-by-value.b7e3ef.png)

The following code sample shows how to apply sorting by value on the pivot table.

```javascript
let pivotTable = spread.getActiveSheet().pivotTables.get("PivotTable");
var sortInfo = {
    sortType: GC.Pivot.SortType.asc,
    sortValueFieldName: "Quantity"
 };
  pivotTable.sort("Category", sortInfo);
```

### Using Designer

You can also sort data through the designer by clicking **More Sort Options..** from the **Filter Dropdown** of a field. This option opens a **Sort Field** dialog where you can select the target field and set the sort type as ascending or descending.

The following image demonstrates how to sort by value using the designer:
![pivot-sort-by-value](https://gcdocumentsitekrblob.blob.core.windows.net/document-site-files/images/df1fe1ee-eb3c-4da7-8c20-a0d8d2b7e734/pivot-sort-by-value.077032.gif)

You can also use the **Sort** option from the context menu of the value area to sort pivot data by values. The context menu provides the following three sort options:

* Sort Smallest to Largest
* Sort Largest to Smallest
* More Sort Options...

While the first two options sort values in ascending and descending order, respectively, the **More Sort Options...** opens a dialog depending on the value area.
The following table describes how this option behaves when accessed through context menu of the following value areas:

| **Value Area** | **Dialog** | **Behavior** |
| ---------- | ------ | -------- |
| Normal Value Area | ![sort-by-value1](https://gcdocumentsitekrblob.blob.core.windows.net/document-site-files/images/df1fe1ee-eb3c-4da7-8c20-a0d8d2b7e734/sort-by-value1.db302b.png?width=250) | The **Sort by Value** dialog appears. |
| Grand Total Area | ![sort-by-value2](https://gcdocumentsitekrblob.blob.core.windows.net/document-site-files/images/df1fe1ee-eb3c-4da7-8c20-a0d8d2b7e734/sort-by-value2.00683a.png?width=250) | The **Sort by Value** dialog appears. However, sort direction is disabled along with the following conditions: <ul><li>In case of grand total column, the direction is top to bottom by default. </li><li>In case of grand total row, the direction is left to right by default. </li><li>In case of grand total row and column, the dialog does not appear. </li></ul> |
| Row Header Area/Corner Area | ![sort-by-value3](https://gcdocumentsitekrblob.blob.core.windows.net/document-site-files/images/df1fe1ee-eb3c-4da7-8c20-a0d8d2b7e734/sort-by-value3.24e54b.png?width=250) | Opens the **Sort Field** dialog along with the following conditions: <ul><li><span style="color: rgb(23, 43, 77); font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Fira Sans, Droid Sans, Helvetica Neue, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.08px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">In the case of compact layout, the </span>**<span style="color: rgb(23, 43, 77); font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Fira Sans, Droid Sans, Helvetica Neue, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.08px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">Sort</span>**<span style="color: rgb(23, 43, 77); font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Fira Sans, Droid Sans, Helvetica Neue, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.08px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> option does not appear in the corner area. </span></li><li>If the selected field is ΣValues field, the **Sort by Value** dialog appears. </li><li>If the selected row area is<span style="color: rgb(23, 43, 77); font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Fira Sans, Droid Sans, Helvetica Neue, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.08px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> ΣValues item, no dialog appears. </span></li></ul> |
| Column Header Area | ![sort-by-value4](https://gcdocumentsitekrblob.blob.core.windows.net/document-site-files/images/df1fe1ee-eb3c-4da7-8c20-a0d8d2b7e734/sort-by-value4.ff4ac7.png?width=250) | Opens the **Sort Field** dialog along with the following conditions: <ul><li>If the selected field is ΣValues field, the **Sort by Value** dialog appears. </li><li>If the selected column area is<span style="color: rgb(23, 43, 77); font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Fira Sans, Droid Sans, Helvetica Neue, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: -0.08px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> ΣValues item, no dialog appears. </span></li></ul> |

## Sort by Custom Field Item Value

To sort pivot table data by a custom field item value, you can use `sortByPivotReferences` property of the `sortInfo` object.

The below image demonstrates ascending sort on the **Category** field as per the data in **Qtr1** of the **OrderDate** field.
![pivot-sort-by-custom-value](https://gcdocumentsitekrblob.blob.core.windows.net/document-site-files/images/df1fe1ee-eb3c-4da7-8c20-a0d8d2b7e734/pivot-sort-by-custom-value.58dbfa.png)

The following code sample shows how to set sorting by custom field item value:

```javascript
let pivotTable = spread.getActiveSheet().pivotTables.get("PivotTable");
 var sortInfo = {
  sortType: GC.Pivot.SortType.asc,
  sortValueFieldName: "Quantity",
  sortByPivotReferences: [
     {
        fieldName: "OrderDate",
        items: ["Qtr1"]
     }
    ]
}
pivotTable.sort("Category", sortInfo);
```

### Using Designer

You can also sort data through the designer by clicking **More Sort Options...** from the **Filter Dropdown** of a <span style="color: #181818">field.</span> This option opens a **Sort Field** dialog where you select the sort order for the target field and then click **More Options...** button to open another **Sort** dialog. On this dialog, by default, GrandTotal is selected, which gives the same results as that of sort by value. For sorting by custom field item value, select a range using the range selector of **Values in selected column** and click **OK.**

> The dialog options are disabled for a row or column area field. For example, in this case, if you click More Options.. button for Category field, it will be disabled, so you must select Quantity field and then click the **More Options...** button.

The following image demonstrates how to sort by custom field item value using the designer:
![pivot-sort-by-custom-field-value](https://gcdocumentsitekrblob.blob.core.windows.net/document-site-files/images/df1fe1ee-eb3c-4da7-8c20-a0d8d2b7e734/pivot-sort-by-custom-field-value.47e35c.gif)

## Sort by Custom Callback

To sort pivot table data using custom logic, instead of sorting in ascending or descending order of values, you can use the custom callback function.

The below image demonstrates the sorting of the **TotalSales** column by using a custom callback function.
![pivot-sort-by-custom-callback](https://gcdocumentsitekrblob.blob.core.windows.net/document-site-files/images/df1fe1ee-eb3c-4da7-8c20-a0d8d2b7e734/pivot-sort-by-custom-callback.ac07df.png)

The following code sample shows sorting on the TotalSales column by using a custom callback:

```javascript
let pivotTable = spread.getActiveSheet().pivotTables.get("PivotTable");
 var sortInfo = {
       sortType: GC.Pivot.SortType.desc,
       customSortCallback: (fieldItemNameArray)=>{
       alert("sorting from custom callback")
       return fieldItemNameArray.sort((a,b)=>b-a);
    }
  };
pivotTable.sort("TotalSales", sortInfo);
```