# How to sort by LocalDateTime

**URL:** https://discourse.hibernate.org/t/how-to-sort-by-localdatetime/6856
**Category:** Hibernate Search
**Created:** [October 31, 2022, 7:29pm UTC](https://discourse.hibernate.org/t/how-to-sort-by-localdatetime/6856 "2022-10-31T19:29:55Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Anni\_Carneiro](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/anni_carneiro/32/2092_2.png) [@Anni\_Carneiro](https://discourse.hibernate.org/u/Anni_Carneiro)
#### Post date: [October 31, 2022, 7:29pm UTC](https://discourse.hibernate.org/t/how-to-sort-by-localdatetime/6856/1 "2022-10-31T19:29:55Z")

</div>

Hello I have a list of activities `List inside an main entity:

The property “activities” inside the main entity is annotated with @IndexedEmbedded

Here is my ActivitityEntity:

```auto
  @Field(name = "date_sort", index = Index.YES, analyze = Analyze.NO, store = Store.YES, termVector = TermVector.YES, indexNullAs = Field.DEFAULT_NULL_TOKEN)
  @SortableField(forField = "date_sort")
  private LocalDateTime startDateTime;

```

How can I sort by this field?

```auto
    Sort sort = new Sort(
        new SortField("activities.startDateTime", Type.SCORE, Boolean.FALSE));

```

With score type it is sorting but incorrectly, with other types I am receiving some errors…  
And it works only if I added the “name” and “forField” attributes…

Thanks

---

<div class="post-metadata">

### Author: ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)
#### Post date: [November 2, 2022, 7:30am UTC](https://discourse.hibernate.org/t/how-to-sort-by-localdatetime/6856/2 "2022-11-02T07:30:54Z")

</div>

Hey,

I’d say don’t try to guess at the internals and just use the [Hibernate Search DSL](https://docs.jboss.org/hibernate/search/5.11/reference/en-US/html_single/#query-sorting) 🙂

EDIT: Also, you’re using the wrong field name in your sort. You should use `activities.date_sort`, not `activities.startDateTime`.

EDIT: Also, your field is multi-valued so this won’t work in Hibernate Search 5; see my [other answer below](https://discourse.hibernate.org/t/how-to-sort-by-localdatetime/6856/5).

```java
FullTextSession fullTextSession = Search.getFullTextSession(session);
QueryBuilder builder = fullTextSession.getSearchFactory()
    .buildQueryBuilder().forEntity(MyEntity.class).get();
Query luceneQuery = /* ... */;
FullTextQuery query = s.createFullTextQuery( luceneQuery, MyEntity.class );
Sort sort = builder
  .sort()
    .byField("activities.date_sort")
  .createSort();
query.setSort(sort);
List results = query.list();

```

It would be even [easier and arguably more straightforward with Hibernate Search 6+](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#search-dsl-sort-concepts):

```java
SearchSession searchSession = Search.session( entityManager );

List<Book> result = searchSession.search( MyEntity.class ) 
        .where( f -> /* ... */ )
        .sort( f -> f.field( "activities.date_sort" ) )
        .fetchHits( 20 ); 

```

---

<div class="post-metadata">

### Author: ![Anni\_Carneiro](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/anni_carneiro/32/2092_2.png) [@Anni\_Carneiro](https://discourse.hibernate.org/u/Anni_Carneiro)
#### Post date: [November 3, 2022, 4:56pm UTC](https://discourse.hibernate.org/t/how-to-sort-by-localdatetime/6856/3 "2022-11-03T16:56:28Z")

</div>

Hi I tried with this first solution, but it’s showed this error:

```auto
`2022-11-03 13:54:07.638 ERROR 15476 --- [nio-8092-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: unexpected docvalues type NONE for field 'activities.startTime' (expected=SORTED). Use UninvertingReader or index with docvalues.; nested exception is java.lang.IllegalStateException: unexpected docvalues type NONE for field 'activities.startTime' (expected=SORTED). Use UninvertingReader or index with docvalues.] with root cause

java.lang.IllegalStateException: unexpected docvalues type NONE for field 'activities.startTime' (expected=SORTED). Use UninvertingReader or index with docvalues.
	at org.apache.lucene.index.DocValues.checkField(DocValues.java:208) ~[lucene-core-5.5.5.jar:5.5.5 b3441673c21c83762035dc21d3827ad16aa17b68 - sarowe - 2017-10-20 08:57:09]
	at org.apache.lucene.index.DocValues.getSorted(DocValues.java:264) ~[lucene-core-5.5.5.jar:5.5.5 b3441673c21c83762035dc21d3827ad16aa17b68 - sarowe - 2017-10-20 08:57:09]
`

```

---

<div class="post-metadata">

### Author: ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)
#### Post date: [November 4, 2022, 7:54am UTC](https://discourse.hibernate.org/t/how-to-sort-by-localdatetime/6856/4 "2022-11-04T07:54:23Z")

</div>

Ah. Well, yes, your example wasn’t using the right field name and I copy-pasted without thinking. You should use `activities.date_sort`, not `activities.startDateTime`. I’ll update my answer.

---

<div class="post-metadata">

### Author: ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)
#### Post date: [November 4, 2022, 8:00am UTC](https://discourse.hibernate.org/t/how-to-sort-by-localdatetime/6856/5 "2022-11-04T08:00:24Z")

</div>

Actually, I just noticed there are multiple activities, and thus multiple “startDateTime”, per indexed entity.

This just won’t work in Hibernate Search 5, because Hibernate Search 5 only supports sorting on single-valued fields. There is no workaround.

You will have to upgrade to Hibernate Search 6, which [does support sorting on multi-valued fields](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#search-dsl-sort-common-multi-value-mode).

You will find an extensive [migration guide from Hibernate Serach 5 to 6 here](https://docs.jboss.org/hibernate/search/6.0/migration/html_single/).
