---
title: "Sorting facets"
slug: "sorting-facets"
abstract: "Advanced examples that show you how to use the Sitecore Discover Search and Recommendation API to sort facets."
---
# Sorting facets

> [!note]
> For basic examples, see [Using faceted search](https://doc.sitecore.com/discover/en/developers/discover-developer-guide/integrate-using-discover-rest-apis/integrate-using-the-search-and-recommendation-api/examples-of-advanced-search-and-recommendation-use-cases/using-faceted-search.html "Using faceted search").

If you want to sort facets in a different order from the rest of the response, use the `facet.sort` key. The specified sorting order applies to all facets enclosed within the object.

To overwrite `facet.sort` for a particular facet, add a sort object to that facet, as the [Using different sorting for different facets examples shows](https://doc.sitecore.com/discover/en/developers/discover-developer-guide/integrate-using-discover-rest-apis/integrate-using-the-search-and-recommendation-api/examples-of-advanced-search-and-recommendation-use-cases/sorting-facets.html#UUID-61b0af84-14c1-29ee-b5e9-231180a22405_section-idm4504900002667232884624839664 "Using different sorting for different facets").

The following examples continue the [getting facet values from each facet type example](https://doc.sitecore.com/discover/en/developers/discover-developer-guide/integrate-using-discover-rest-apis/integrate-using-the-search-and-recommendation-api/examples-of-advanced-search-and-recommendation-use-cases/using-faceted-search.html#UUID-c83dce98-05b9-df40-143c-aa7e7ffc5820_section-idm4652146060729632877707930886 "Getting facet values from each facet type").

<details>
<summary><h2>Sorting all facets by text alphabetically</h2></summary>

The following shows an example of sorting all facets by text. In this example, you request three facets from each `<facet_type>` and then sort all facets alphabetically.

Here is the request:

```JSON
{
  "facet": {
    "all": true,
    "max": 3,
    "sort": {
      "type": "text",
      "order": "desc"
    }
  }
}
```

Here is the response:

```JSON
{
  "ts": 1480977544,
  "rid": "response_id_fo1",
  "facet": {
    "colors": {
      "value": [
        {"id": "color_id_blue", "text": "Blue", "count": 11},
        {"id": "color_id_red", "text": "Red", "count": 23},
        {"id": "color_id_yellow", "text": "Yellow", "count": 10}
      ]
    },
    "product_type": {
      "value": [
        {"id": "prod_type_id_earring", "text": "Earring", "count": 535},
        {"id": "prod_type_id_necklace", "text": "Necklace", "count": 45},
        {"id": "prod_type_id_ring", "text": "Ring", "count": 105}
      ]
    },
    "primary_stone_type": {
      "value": [
        {"id": "stone_id_crystal", "text": "Crystal", "count": 779},
        {"id": "stone_id_diamond", "text": "Diamond", "count": 2374},
        {"id": "stone_id_emerald", "text": "Emerald", "count": 50}
      ]
    },
    "primary_metal_type": {
      "value": [
        {"id": "stone_id_aluminum", "text": "Aluminum", "count": 4},
        {"id": "stone_id_brass", "text": "Brass", "count": 80},
        {"id": "stone_id_gold", "text": "Gold", "count": 45}
      ]
    }
  },
  "facet_names": ["colors", "product_type", "primary_stone_type", "primary_metal_type"]
}
```

</details>

<details>
<summary><h2>Using different sorting for different facets</h2></summary>

The following shows a request that uses different types of sorting for different facets. Here, you want to:

- Sort `colors` facets by *count* in descending order
- Sort all other facets by text in ascending order
- Get the `total` for all facet types.

In this example, you request three facets from each `<facet_type>` . Then, you sort the `colors` facets by *count* in descending order.

Here is the request:

```JSON
{
  "facet": {
    "all": true,
    "max": 3,
    "total": true,
    "sort": {
      "type": "text",
      "order": "desc"
    },
    "colors": {
      "sort": {
        "type": "count",
        "order": "desc"
      }
    }
  }
}
```

Here is the response:

```JSON
{
  "ts": 1480977544,
  "rid": "response_id_f02",
  "facet": {
    "colors": {
      "total": 11,
      "value": [
        {"id": "color_id_red", "text": "Red", "count": 23},
        {"id": "color_id_blue", "text": "Blue", "count": 11},
        {"id": "color_id_yellow", "text": "Yellow", "count": 10}
      ]
    },
    "product_type": {
      "total": 30,
      "value": [
        {"id": "prod_type_id_earring", "text": "Earring", "count": 535},
        {"id": "prod_type_id_necklace", "text": "Necklace", "count": 45},
        {"id": "prod_type_id_ring", "text": "Ring", "count": 105}
      ]
    },
    "primary_stone_type": {
      "total": 80,
      "value": [
        {"id": "stone_id_crystal", "text": "Crystal", "count": 779},
        {"id": "stone_id_diamond", "text": "Diamond", "count": 2374},
        {"id": "stone_id_emerald", "text": "Emerald", "count": 50}
      ]
    },
    "primary_metal_type": {
      "total": 36,
      "value": [
        {"id": "stone_id_aluminum", "text": "Aluminum", "count": 4},
        {"id": "stone_id_brass", "text": "Brass", "count": 80},
        {"id": "stone_id_gold", "text": "Gold", "count": 45}
      ]
    }
  },
  "facet_names": ["colors", "product_type", "primary_stone_type", "primary_metal_type"]
}
```

</details>