+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Search result sort by date

Search result sort by date

4 posts, 0 answered
  1. Ana
    Ana avatar
    8 posts
    Registered:
    24 Aug 2010
    14 Dec 2010
    Link to this post
    Hi there,

    Is  there any way to sort search results  (some articles) by  created date if I am using CustomIndexProvider to index custom data that stored in different table?

    Thank you,
    Ana.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    14 Dec 2010
    Link to this post
    Hi Ana,

    You need a custom search results control where you can sort the output. You need to use some criteria on which you will identify the items and then sort them. You could take a look at


    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Ana
    Ana avatar
    8 posts
    Registered:
    24 Aug 2010
    16 Dec 2010
    Link to this post
    Thank you for your reply.

    So I am using my custom control.But I still didn't get how I can sort my result.
    So here I am getting result for news items search:

    IList<ResultItem> newList = new List<ResultItem>();
    IList<ResultItem> CustomDataSource = SearchManager.Search(
     searchQuery,
     CatalogName,
     startIndex,
     this.PostsPerPage,
     this.WordsMode,
     this.EscapeSpecialChars,
     out totalItems2);

    foreach (ResultItem customItem in CustomDataSource)
    {
       newList.Add(customItem);
    }

    After that basically i need to sort it out by date.


    Thank you,
    Ana






  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    16 Dec 2010
    Link to this post
    Hello Ana,

    Create a custom interface or class which will extend the ResultItem type so you will have a property that will be used for filtering. You have to move the ResultItems to this custom type and then filter the list based on some property of it.

    1. You need to create a  property of type DateTime so you will add the date of the content item to this property
    2.You have to create a new list of your custom type that extends ResultItem and bind the Repeater control to it.

    here is how to extract the content item ID from the snippet ( after you have followed Search to Provide Article ID)

    foreach (ResultItem itm in allItems)
        {
            if (itm.Snippet.Length >= 36)
            {
                var id = itm.Snippet.Substring(0, 36);
                if (IsGuid(id))
                {
                    var cItem = manager.GetContent(new Guid(id));
                    if (cItem != null)
                    {
                        newsList.Add(cItem);
                    }
                }
      
                else
                {
                    var cItem = ResolveItemByUrl(itm.Title);
                    if (cItem != null)
                    {
                        newsList.Add(cItem);
                    }
                }
            }
      
            else
            {
                var cItem = ResolveItemByUrl(itm.Title);
                if (cItem != null)
                {
                    newsList.Add(cItem);
                }
            }
        }
        return newsList;
    }
      
      
    public IContent ResolveItemByUrl(string title)
    {
        var newsManager = new ContentManager("News");
        var content = newsManager.GetContent().Cast<CmsContentBase>().Where(c => c.GetMetaData("Title").ToString() == title.Trim()).SingleOrDefault();
        if (content != null)
            return content;
        return null;
    }
      
    internal static bool IsGuid(string candidate)
    {
        bool isValid = false;
        if (candidate != null)
        {
            if (isGuid.IsMatch(candidate))
            {
      
                isValid = true;
            }
        }
        return isValid;
    }
      
    private static Regex isGuid = new Regex(@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$", RegexOptions.Compiled);


    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
4 posts, 0 answered