---
title: "Sorting facets"
slug: "sorting-facets"
abstract: "Learn how to use the Search and Recommendation API for Sitecore Search to sort facets."
---
# Sorting facets

You can add a `sort` object to your request to sort facets, that is, determine the order in which facets and facet values appear. Depending on how you nest the `sort` object, you can sort facets at two levels:

- The facet level - use the `widget.items.search.facet.sort` object to apply sorting to all facets and to determine the order in which facet types appear.
- The facet type level - use the `widget.items.search.facet.types.sort` object to apply sorting to a specific facet type and to determine the order in which facet type values appear within each facet type.

For the data model of these objects, see the [Search and Recommendation API reference](https://doc.sitecore.com/search/en/developers/search-and-recommentation-api/index.html).

You can sort by ascending or descending order of `count`, that is, by the number of items in a facet type or facet type value, or by `text`, that is, alphabetically.

## Sorting at the facet level

To sort at the facet level, use the `widget.items.search.facet.sort` object and pass the sorting options you want to apply.

In this example, you want to sort all facet types in descending order of the number of items for each facet type. To do this, pass `count` as the `name` and `desc` as the `order`.

The following is the sample request:

```JSON
{
    "widget": {
        "items": [
            {
                "rfk_id": "rfkid_7",
                "entity": "content",
                "sources": [
                    "388218",
                    "390565"
                ],
                "search": {
                    "facet": {
                        "sort": {
                            "name": "count",
                            "order": "desc"
                        },
                        "types": [
                            {
                                "name": "type"
                            }
                        ]
                    }
                }
            }
        ]
    },
    "context": {
        "locale": {
            "country": "us",
            "language": "en"
        },
        "user": {
            "uuid": "159871551-1g-i4-4x-1p-f5tdhddqwvywv7b9g2vy-1664852644904"
        }
    }
}
```

The following is the response to the above request:

```JSON
{
    "widgets": [
        {
            "rfk_id": "rfkid_7",
            "entity": "content",
            "facet": [
                {
                    "name": "type",
                    "label": "Type",
                    "value": [
                        {
                            "id": "facetid_eyJ0eXBlIjoiZXEiLCJuYW1lIjoidHlwZSIsInZhbHVlIjoib3RoZXIifQ==",
                            "text": "other",
                            "count": 990
                        },
                        {
                            "id": "facetid_eyJ0eXBlIjoiZXEiLCJuYW1lIjoidHlwZSIsInZhbHVlIjoibmV3cyJ9",
                            "text": "news",
                            "count": 623
                        },
                        {
                            "id": "facetid_eyJ0eXBlIjoiZXEiLCJuYW1lIjoidHlwZSIsInZhbHVlIjoia25vd2xlZGdlIGNlbnRlciJ9",
                            "text": "knowledge center",
                            "count": 424
                        },
                        {
                            "id": "facetid_eyJ0eXBlIjoiZXEiLCJuYW1lIjoidHlwZSIsInZhbHVlIjoid2ViaW5hcnMifQ==",
                            "text": "webinars",
                            "count": 275
                        }
                    ]
                }
            ]
        }
    ],
    "dt": 96,
    "ts": 1669437001203
}
```

In the response `facet` object, you can see that facet types appear in descending order of the number of items they have.

## Sorting at the facet type level

To sort at the page level, use the `widget.items.search.facet.types.sort` object and pass the sorting options you want to apply.

In this example, you want to:

- Sort the values in the `product` facet type in descending alphabetical order. To do this, pass `text` as the `name` and `desc` as the `order`.
- Get a maximum of three values for each facet type. To do this, set `widget.items.search.facet.max` to `3`.

The following is the sample request:

```JSON
{
    "widget": {
        "items": [
            {
                "rfk_id": "rfkid_7",
                "entity": "content",
                "search": {
                    "facet": {
                        "all": true,
                        "max": 3,
                        "types": [
                            {
                                "name": "product",
                                "sort": {
                                    "name": "text",
                                    "order": "desc"
                                }
                            },
                            {
                                "name": "type"
                            }
                        ]
                    }
                }
            }
        ]
    },
    "context": {
        "locale": {
            "country": "us",
            "language": "en"
        },
        "user": {
            "uuid": "159871551-1g-i4-4x-1p-f5tdhddqwvywv7b9g2vy-1664852644904"
        }
    }
}
```

The following is the response to the above request:

```JSON
{
    "widgets": [
        {
            "rfk_id": "rfkid_7",
            "entity": "content",
            "facet": [
                {
                    "name": "type",
                    "label": "Type",
                    "value": [
                        {
                            "id": "facetid_eyJ0eXBlIjoiZXEiLCJuYW1lIjoidHlwZSIsInZhbHVlIjoib3RoZXIifQ==",
                            "text": "other",
                            "count": 990
                        },
                        {
                            "id": "facetid_eyJ0eXBlIjoiZXEiLCJuYW1lIjoidHlwZSIsInZhbHVlIjoibmV3cyJ9",
                            "text": "news",
                            "count": 623
                        },
                        {
                            "id": "facetid_eyJ0eXBlIjoiZXEiLCJuYW1lIjoidHlwZSIsInZhbHVlIjoiTmV3cyJ9",
                            "text": "News",
                            "count": 605
                        }
                    ]
                },
                {
                    "name": "product",
                    "label": "Product",
                    "value": [
                        {
                            "id": "facetid_eyJ0eXBlIjoiZXEiLCJuYW1lIjoicHJvZHVjdCIsInZhbHVlIjoiWE0gQ2xvdWQifQ==",
                            "text": "XM Cloud",
                            "count": 1
                        },
                        {
                            "id": "facetid_eyJ0eXBlIjoiZXEiLCJuYW1lIjoicHJvZHVjdCIsInZhbHVlIjoiU2VuZCJ9",
                            "text": "Send",
                            "count": 19
                        },
                        {
                            "id": "facetid_eyJ0eXBlIjoiZXEiLCJuYW1lIjoicHJvZHVjdCIsInZhbHVlIjoiUGVyc29uYWxpemUifQ==",
                            "text": "Personalize",
                            "count": 14
                        }
                    ]
                }
            ]
        }
    ],
    "dt": 40,
    "ts": 1669436687984
}
```

In the response `facet` object, you can see that facet type values appear in descending alphabetical order for both the `type` and `product` facet types.