How to sort the results as per the published date?

Vote:
 

Hi.

 

I want to sort the results.So, I tried the following code, but unfortunately this is not working for me.

Can you please check and give some suggestions to update this code :

ITypeSearch<PageData> query = client.Search<PageData>()
                    .For(Query)
                    .WithAndAsDefaultOperator()
                    .OrderByDescending(x=>x.StartPublish)
                    .Filter(x => x.StartPublish.LessThan(DateTime.Now))
                    .OrFilter(x => x.StartPublish.Match(DateTime.Now))
                    .Filter(x => x.StopPublish.GreaterThan(DateTime.Now))
                    .Filter(x => x.LanguageID.Match(CurrentPage.LanguageID));

var result = query
                    .Select(x => new SearchHit
                    {
                        PageTitle = x.PageName,
                        Id = x.PageLink.ID,
                        Url = x.LinkURL
                    })
                    .Track()
                    .Take(Count)
                    .GetResult();

  Thanks.

 Kishore

#80179
Jan 16, 2014 9:54
Vote:
 

I would try to have the OrderBy-clause as late as possible. You could for instance remove it from the query =..., and then do something like this:

var result = query.Select(x => new SearchHit
{
   PageTitle = x.PageName,
   Id = x.PageLink.ID,
   Url = x.LinkURL,
   StartPublish = x.StartPublish
})
.Track()
.Take(Count)
.OrderByDescending(x => x.StartPublish)
.GetResult();

#80182
Edited, Jan 16, 2014 10:22
Vote:
 

Kishore, your query looks correct to me. Perhaps you could give a bit more details on what is wrong? How are your results looking? How are they sorted?

Where you put the OrderBy-clause should not matter as it will only add the sort option to the query, which is executed when GetResult() is called. 

#80551
Jan 24, 2014 14:59
Vote:
 

Thank you all.

I solved this issue as per Anders suggestion.

#80712
Jan 30, 2014 11:29
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.