# How can I sort elastic search ui results by date?

**URL:** <https://discuss.elastic.co/t/how-can-i-sort-elastic-search-ui-results-by-date/352341>\
**Category:** Elastic Search\
**Tags:** elastic-site-search\
**Created:** [February 1, 2024, 9:55pm UTC](https://discuss.elastic.co/t/how-can-i-sort-elastic-search-ui-results-by-date/352341 "2024-02-01T21:55:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AaronDem](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aarondem/32/131353_2.png) [@AaronDem](https://discuss.elastic.co/u/AaronDem)\
**Post date:** [February 1, 2024, 9:55pm UTC](https://discuss.elastic.co/t/how-can-i-sort-elastic-search-ui-results-by-date/352341/1 "2024-02-01T21:55:54Z")

</div>

Hello, what happens is that I have a page with react that uses the package:

@elastic/search-ui-site-search-connector

Through the configuration and the query I bring information and already display it visually.

The problem is that I want to sort the results by date but even if I add the sort property, it doesn't do it, I tried to do it even with a simple and more common field as title but it doesn't sort even if I do it.

I try to use sort property like the next way:

"sort": [{"createdPageDate.raw": {"order": "desc"}}]  
sort: [{ createdPageDate: { order: 'desc' } }]  
sort: [{ "createdPageDate": { "order": 'desc' } }]  
"sort": [{ "createdPageDate": { "order": 'desc' } }]  
sort: {createdPageDate : 'desc'},  
sort: {  
field: "createdPageDate",  
order: "desc",  
}

This is the query and config that I use:

```auto
const searchConnector = new SiteSearchAPIConnector({
    engineKey: apikey,
    documentType: "page"
  });

  const config = {
    apiConnector: searchConnector,
    autocompleteQuery: {
      suggestions: {
        types: {
          documents: {
            fields: ["title"]
          }
        },
        size: 4
      }
    },
    searchQuery: {
      search_fields:
              {
                title: {},
                description: {}
              },
      result_fields: {
        title: {
          raw: {}
        },
        createdPageDate: {
          raw: {}
        },
        link: {
          raw: {}
        },
        page_id: {
          raw: {}
        },
        content_type: {
          raw: {}
        },
      },
      facets: {
        content_type: {type: "value"},
        size: {type: "value"}
      },
    },
    alwaysSearchOnInitialLoad: true,
  };

  // RENDER THE RESULT ON VISUAL FORM

<SearchProvider config={config}>
            <Results
                resultView={({result}) => {
                  if (result.content_type !== undefined) {
              // REST OF THE CODE 
             />
</SearchProvider>

```

---

<div class="post-metadata">

**Author:** ![Yan\_Savitski](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yan_savitski/32/126980_2.png) [@Yan\_Savitski](https://discuss.elastic.co/u/Yan_Savitski)\
**Post date:** [February 5, 2024, 1:41pm UTC](https://discuss.elastic.co/t/how-can-i-sort-elastic-search-ui-results-by-date/352341/2 "2024-02-05T13:41:13Z")

</div>

Hi there  
You can specify sort params in the initialState

```auto
initialState: {
    sort: [{ field: "createdPageDate", order: "desc"}]
  },

```

In [RequestState doc](https://docs.elastic.co/search-ui/api/core/state#request-state) you can also read more about other options to change it.

Yan

---

<div class="post-metadata">

**Author:** ![AaronDem](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aarondem/32/131353_2.png) [@AaronDem](https://discuss.elastic.co/u/AaronDem)\
**Post date:** [February 6, 2024, 5:04pm UTC](https://discuss.elastic.co/t/how-can-i-sort-elastic-search-ui-results-by-date/352341/3 "2024-02-06T17:04:39Z")

</div>

Still not working, is necessary to modify something else about the code or the elasticsearch / swiftype settings?

Is a space or specific section of the query where I need to add the code you provide me?

---

<div class="post-metadata">

**Author:** ![Yan\_Savitski](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yan_savitski/32/126980_2.png) [@Yan\_Savitski](https://discuss.elastic.co/u/Yan_Savitski)\
**Post date:** [February 6, 2024, 5:53pm UTC](https://discuss.elastic.co/t/how-can-i-sort-elastic-search-ui-results-by-date/352341/4 "2024-02-06T17:53:41Z")

</div>

Don't need to change anything in elasticsearch/swiftype  
Put this initialState in the root of config

In sandbox example for some reason only works with sortList  
Try to use this code below for [sandbox](https://codesandbox.io/p/sandbox/ancient-tree-32xp9i?file=%2Fsrc%2Fpages%2Felasticsearch%2Findex.js)

```auto
initialState: {
    sortList: [
      {
        field: "title.keyword",
        direction: "desc",
      },
    ],
  },

```

Yan

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 5, 2024, 5:53pm UTC](https://discuss.elastic.co/t/how-can-i-sort-elastic-search-ui-results-by-date/352341/5 "2024-03-05T17:53:56Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
