Changing the order of the search results

It is possible to sort the search results in ascending or descending order.

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. You do not have to add the same field as a filter tag, but the field has to be present in TagNames, and the field type has to be defined in the SortingFieldType enumeration.

Is this page helpful?
Thank you for your feedback!