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.

The following example searches for all pages with names starting with "Euclid", and then sorts them in 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 enumeration defines the type of the field. It is not mandatory that the same field is added as a filter tag, it is only required that the field is present in TagNames and that the field type is defined in the SortingFieldType enumeration.

Is this page helpful?
Thank you for your feedback!