Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4248842
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:18:56+00:00 2026-05-21T04:18:56+00:00

I am unsure that how I can use it with sorting. I want to

  • 0

I am unsure that how I can use it with sorting. I want to sort the result by datetime descending.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:18:57+00:00Added an answer on May 21, 2026 at 4:18 am

    UPDATE (2013.04.22): Newer versions of Lucene.NET allow sorting by DateTime more directly, for example:

    var sort = new Sort( new SortField( Constants.LuceneCreationTime, SortField.LONG, true ) );
    var filter = new QueryWrapperFilter( query );
    var docs = searcher.Search( query, filter, 100, sort )
    

    Add the CreationTime as DateTime.Ticks to allow searching for it via SortField.LONG.

    I have tested this with version 3.0.


    yes, this question has partly been answered before. However, I would like to add a note on the DateTime part.

    In regular Lucene communities it is offen recommended to split up DateTime’s in order to make them performant and/or precise enough (Int32 is just 10 characters). I’ve benchmarked with up to 500.000 documents half a year ago, and as I remember, this really was the only performant way I could come up with.

    Anyway, you can try out something like this:

    Indexing

    var indexWriter = OpenWritableIndex();
    Document doc = new Document();
    
    DateTime lastEdited = DateTime.Now;
    int year = lastEdited.Year;
    int month = lastEdited.Month;
    int day = lastEdited.Day;
    int hour = lastEdited.Hour;
    int minute = lastEdited.Minute;
    
    doc.Add(new Field("LastEditedYear", year.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
    doc.Add(new Field("LastEditedMonth", month.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
    doc.Add(new Field("LastEditedDay", day.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
    doc.Add(new Field("LastEditedHour", hour.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
    doc.Add(new Field("LastEditedMinute", minute.ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
    
    indexWriter.AddDocument(doc);   
    

    Searching

    var readableIndex = OpenReadableIndex();
    var searcher = new IndexSearcher(readableIndex.Directory);
    var multiParser = new MultiFieldQueryParser(termsToSearchIn, readableIndex.Analyzer);
    
    var query = multiParser.Parse(terms);
    Hits hits = null;
    Sort sort = new Sort(new SortField[]
    {
        new SortField("LastEditedYear", true),
        new SortField("LastEditedMonth", true),
        new SortField("LastEditedDay", true),
        new SortField("LastEditedHour", true),
        new SortField("LastEditedMinute", true)
    }); 
    if(sort != null)
    {
        try
        {
            hits = searcher.Search(query, sort);
        }
        catch(SystemException) // Lucene throws a SystemException when trying to sort an empty response.
        {
            return new List<string>();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a url that contains valid xml, but unsure on how I could
I've just started using NHibernate, and I have some issues that I'm unsure how
There are a lot of different classes that can be used in various ways
I am using Netbeans 6.8 as my Python IDE. I can use PIL (1.1.7)
I'm looking to make a service which I can use to make calls to
I have a C function that returns an unsigned char * that can either
I want to be able to communicate between two machines that are running Windows.
I have a string of test like this: <customtag>hey</customtag> I want to use a
I want to allow users of an iPhone app to upload photos and use
I want to allow users of an iPhone app to upload photos and use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.