---
title: SortBy Options
slug: template-js/sortby-options
docTags: 
createdAt: 2022-07-12T02:20:27.000Z
---



The default SortBy functionality can be modified within Template JS by overriding the default Script block. Additional supported options can be added as well as edits to the default labeling.

See : [Custom Overrides](docId:_bc3GcThleEaToYujDtuE) for details on preparing for Klevu JS overriding

See : An in-action example and the source code on the [Klevu CodeSandbox](https://codesandbox.io/s/change-sorting-options-867krq)

## Indexing

:::hint{type="success"}
Supported options for product sorting must be supplied at the product level

- price / sale\_price
- title
- rating
- id / date\* (for newness)&#x20;
:::

\*Please see this [support article](https://help.klevu.com/support/solutions/articles/5000871480-sort-order-options) for important information about sorting by newness.



## API Reference

| **Parameter**                         | **Description**                                                                               |
| ------------------------------------- | --------------------------------------------------------------------------------------------- |
| RELEVANCE                             | Default combination of Klevu A.I. and merchandising configuration                             |
| PRICE\_ASC, PRICE\_DESC               | Sort the results by the `salePrice` value of each record                                      |
| NAME\_ASC, NAME\_DESC                 | Sort the results by the `name` of each record, in alphabetical order                          |
| RATING\_ASC, RATING\_DESC             | Sort the results by each record's average rating, if this data has been indexed in your store |
| NEW\_ARRIVAL\_ASC, NEW\_ARRIVAL\_DESC | Sort your records based on their published date.                                              |

See :  Klevu API Documentation for available [Sorting Options](https://docs.klevu.com/apis/smart-search-configurations)



## Template JS Override

The SortBy options template block can be modified using the [Set Template](docId:6YVX9naw6AX68WYguCOB7) override.  The [Set Custom Helpers](docId\:Q0fQR_0VzOsbxRbf7RS_L) are used to modify the desired display text of the selected option as well.

:::CodeblockTabs
JavaScript

```html
<script type="text/javascript">

klevu({
	theme: {
      setTemplates: {
		 myCustomSortBy: {
		  scope: "landing,catnav", 
		  selector: "#myCustom_klevuLandingTemplateSortBy",
		  name: "sortBy"
		 }
	  },
	  setHelpers: {
        "landing,catnav": {
            myCustomSortBy: function(dataLocal) {
              /* sets the selected option to the display */

              var selectedText = "";

              var klevuStorage = klevu.getSetting(klevu.settings, "settings.storage");

              switch (klevuStorage.sort.getElement(dataLocal) == dataLocal ? "RELEVANCE" : klevuStorage.sort.getElement(dataLocal)) {
                case "RELEVANCE":
                    selectedText = "Best Match";
                    break;
                case "PRICE_ASC":
                    selectedText = "Price: Low to high";
                    break;
                case "PRICE_DESC":
                    selectedText = "Price: High to low";
                    break;
                case "NAME_ASC":
                    selectedText = "Name: A to Z";
                    break;
                case "NAME_DESC":
                    selectedText = "Name: Z to A";
                    break;
                case "NEW_ARRIVAL_DESC":
                    selectedText = "Newest to Oldest";
                    break;
                case "NEW_ARRIVAL_ASC":
                    selectedText = "Oldest to Newest";
                    break;
                default:
                    selectedText = "Best Match";
              }
              return selectedText;
          }
        }
      }
	}
});
</script>


<script type="template/klevu" id="myCustom_klevuLandingTemplateSortBy">
  <div class="kuDropdown kuDropSortBy" role="listbox">
    <div class="kuDropdownLabel">
      <%=helper.translate("Sort by : ")%><%=helper.myCustomSortBy(dataLocal)%>
    </div>
    <div class="kuDropdownOptions">
      <div class="kuDropOption kuSort" data-value="RELEVANCE" role="option">Best Match</div>
      <div class="kuDropOption kuSort" data-value="PRICE_ASC" role="option">Price: Low to high</div>
      <div class="kuDropOption kuSort" data-value="PRICE_DESC" role="option">Price: High to low</div>
      <div class="kuDropOption kuSort" data-value="NAME_ASC" role="option">Name: A to Z</div>
      <div class="kuDropOption kuSort" data-value="NAME_DESC" role="option">Name: Z to A</div>
      <div class="kuDropOption kuSort" data-value="NEW_ARRIVAL_DESC" role="option">Newest to Oldest</div>
      <div class="kuDropOption kuSort" data-value="NEW_ARRIVAL_ASC" role="option">Oldest to Newest</div>
  </div>
 </div>
</script>


```
:::

