Changing search results sort order

It is possible to sort the search results in ascending or descending order. This article explains how.

To sort the search result, add the Sortings artifacts to the search request.

Following example searches for all pages with names starting from "Euclid" and then sort them in the ascending order by name.

       /// <summary>
        /// Searches for all pages starting with short name 'Euclid' and sort the results in ascending order.
        /// </summary>
        /// <param name="website">The website.</param>
        /// <param name="token">The token.</param>
        /// <returns></returns>
        public static List<Guid> SearchForEuclid(WebSite website, SecurityToken token)
            List<Guid> pages = null;
            var searchRequest = new QueryRequest(website.Language.ID, CmsSearchDomains.Pages, token);
            searchRequest.FilterTags.Add(new Tag(TagNames.WebSiteId, website.ID));

            //we search for all pages that has a short name starting with Euclid.
            searchRequest.FilterTags.Add(new Tag(TagNames.Name, "Euclid*"));

            //add the sorting direction, so the results are sorted according to ascending direction.
            searchRequest.Sortings.Add(new Sorting(TagNames.Name, SortDirection.Ascending, SortingFieldType.String));

            var response = Solution.Instance.SearchService.Search(searchRequest);
            if (response.Hits.Count > 0)
                pages = (from hit in response.Hits select new Guid(hit.Id)).ToList();
            return pages ?? new List<Guid>();

The SortingFieldType enumaration defines the type of the field. It is not mandatory that the same field be added as a filter tag, but its only required that the field is present in TagNames and the fields type is defined in the SortingFieldType enumeration.

Is this page helpful?
Thank you for your feedback!