# How to sort ResultView data

**URL:** <https://discuss.elastic.co/t/how-to-sort-resultview-data/288798>\
**Category:** Elastic Search\
**Tags:** elastic-app-search\
**Created:** [November 9, 2021, 4:48pm UTC](https://discuss.elastic.co/t/how-to-sort-resultview-data/288798 "2021-11-09T16:48:34Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![pradeepjanga](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@pradeepjanga](https://discuss.elastic.co/u/pradeepjanga)\
**Post date:** [November 9, 2021, 4:48pm UTC](https://discuss.elastic.co/t/how-to-sort-resultview-data/288798/1 "2021-11-09T16:48:34Z")

</div>

I have custom result view but i want to do some sorting before data is being rendered.  
is that possible?

```auto
 <React.Fragment>
            {wasSearched && (totalResults > 0) ?
                <CurrentFilterSelection filters={filters}/>:
                <></> }
            {(totalResults > 0) ? ( <Results
            titleField={getConfig().titleField}
            urlField={getConfig().urlField}
            shouldTrackClickThrough={true}
            resultView={ResultView}
        />): wasSearched && !clearSelected ? <h2>No results found; please change your search criteria</h2>:''}
        </React.Fragment>

```

I want to sort on specific field of the Results so i can get what i want to top of the result view.

---

<div class="post-metadata">

**Author:** ![JasonStoltz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasonstoltz/32/49893_2.png) [@JasonStoltz](https://discuss.elastic.co/u/JasonStoltz)\
**Post date:** [November 9, 2021, 8:42pm UTC](https://discuss.elastic.co/t/how-to-sort-resultview-data/288798/2 "2021-11-09T20:42:11Z")

</div>

If you have a custom `resultView`, then you can sort the fields however you would like inside of the `ResultView` code, right?

---

<div class="post-metadata">

**Author:** ![pradeepjanga](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@pradeepjanga](https://discuss.elastic.co/u/pradeepjanga)\
**Post date:** [November 9, 2021, 9:16pm UTC](https://discuss.elastic.co/t/how-to-sort-resultview-data/288798/3 "2021-11-09T21:16:00Z")

</div>

for example i have a field status and i want to sort before the resultView is populated because inside the result view it is not working fine

---

<div class="post-metadata">

**Author:** ![JasonStoltz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasonstoltz/32/49893_2.png) [@JasonStoltz](https://discuss.elastic.co/u/JasonStoltz)\
**Post date:** [November 10, 2021, 1:07pm UTC](https://discuss.elastic.co/t/how-to-sort-resultview-data/288798/4 "2021-11-10T13:07:37Z")

</div>

There's no need to sort it before it goes into the result view. Just sort it in the result view before you render it.

---

<div class="post-metadata">

**Author:** ![pradeepjanga](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@pradeepjanga](https://discuss.elastic.co/u/pradeepjanga)\
**Post date:** [November 11, 2021, 4:44pm UTC](https://discuss.elastic.co/t/how-to-sort-resultview-data/288798/5 "2021-11-11T16:44:31Z")

</div>

Here is my other example.  
If we search with any searchTerm, we could multiple results and we are displaying them like below:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/f/e/fe4e94374795b80da47aebb407762a169c38b6dd.png)

my resultView is like below:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/0/20026b17b6aeb393c281923749d9707d54300dad.png)

If i add the sorting logic it is executing with each result not the overall search results. I need to sort on overall search results with a specific field. where can i do that ?

---

<div class="post-metadata">

**Author:** ![Scrilling](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/scrilling/32/78663_2.png) [@Scrilling](https://discuss.elastic.co/u/Scrilling)\
**Post date:** [November 12, 2021, 12:20am UTC](https://discuss.elastic.co/t/how-to-sort-resultview-data/288798/6 "2021-11-12T00:20:23Z")

</div>

To add a sort to your search request, assign the `setSort` [action](https://github.com/elastic/search-ui/blob/master/ADVANCED.md#actions) on your `SearchProvider` instance to the field you wish to sort on. It does not matter that you are using a custom `ResultsView` , results will be sorted before rendering. Does this answer your question, or are you looking to reorder the fields within an individual result card?

---

<div class="post-metadata">

**Author:** ![pradeepjanga](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@pradeepjanga](https://discuss.elastic.co/u/pradeepjanga)\
**Post date:** [November 12, 2021, 1:52am UTC](https://discuss.elastic.co/t/how-to-sort-resultview-data/288798/7 "2021-11-12T01:52:42Z")

</div>

we want to implement some custom sorting on one of the field.  
ex: ` const siteStatuses = result.actual_status? result.actual_status.raw:[];`

This siteStatuses is one of the array of strings like ["Completed", "Accruing", "Closed", "open"].  
Now we want to sort the results, such that all open status are top then Accruing then completed like so. something like this custom order ["Open", "Accruing", "Completed", "Closed"]

---

<div class="post-metadata">

**Author:** ![pradeepjanga](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@pradeepjanga](https://discuss.elastic.co/u/pradeepjanga)\
**Post date:** [November 12, 2021, 9:17pm UTC](https://discuss.elastic.co/t/how-to-sort-resultview-data/288798/8 "2021-11-12T21:17:30Z")

</div>

any idea or other workaround to achieve this problem?

---

<div class="post-metadata">

**Author:** ![JasonStoltz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasonstoltz/32/49893_2.png) [@JasonStoltz](https://discuss.elastic.co/u/JasonStoltz)\
**Post date:** [November 15, 2021, 3:41pm UTC](https://discuss.elastic.co/t/how-to-sort-resultview-data/288798/9 "2021-11-15T15:41:51Z")

</div>

App Search doesn't provide a way to do a sort like that.

I would recommend re-indexing your data and applying a custom "sort" field that is based on `siteStatus`.

So your data might look like:

```auto
[{
  id: "1",
  siteStatus: "Closed",
  siteStatusSort: 4
},{
  id: "2",
  siteStatus: "Open",
  siteStatusSort: 1
}]

```

This lets you easily apply a sort on the siteStatusSort field as Sarah mentioned.

Alternatively, you could use "boosts" to influence the order: [Relevance Tuning Guide, Weights and Boosts | Elastic App Search Documentation [7.15] | Elastic](https://www.elastic.co/guide/en/app-search/current/relevance-tuning-guide.html#relevance-tuning-guide-weights-and-boosts-boosts).

You could put a high boost value on any documents with the status "Open", etc.

---

<div class="post-metadata">

**Author:** ![pradeepjanga](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@pradeepjanga](https://discuss.elastic.co/u/pradeepjanga)\
**Post date:** [November 17, 2021, 3:22pm UTC](https://discuss.elastic.co/t/how-to-sort-resultview-data/288798/10 "2021-11-17T15:22:19Z")

</div>

will this work ?

```auto
apiConnector: connector,

    alwaysSearchOnInitialLoad: false,

    initialState:{

      sort: {

        field: 'study_status_order'

      }

    },

    onSearch: async (requestState, queryConfig, next) => {

```

---

<div class="post-metadata">

**Author:** ![pradeepjanga](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@pradeepjanga](https://discuss.elastic.co/u/pradeepjanga)\
**Post date:** [November 17, 2021, 4:23pm UTC](https://discuss.elastic.co/t/how-to-sort-resultview-data/288798/11 "2021-11-17T16:23:13Z")

</div>

that didn't sort. how to use setSort on specific field and direction ?

---

<div class="post-metadata">

**Author:** ![JasonStoltz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasonstoltz/32/49893_2.png) [@JasonStoltz](https://discuss.elastic.co/u/JasonStoltz)\
**Post date:** [November 17, 2021, 5:15pm UTC](https://discuss.elastic.co/t/how-to-sort-resultview-data/288798/12 "2021-11-17T17:15:55Z")

</div>

You should read the manual. Make sure you use the version of the docs matching the version you are using: [search-ui/ADVANCED.md at v1.7.0 · elastic/search-ui · GitHub](https://github.com/elastic/search-ui/blob/v1.7.0/ADVANCED.md)

---

<div class="post-metadata">

**Author:** ![pradeepjanga](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@pradeepjanga](https://discuss.elastic.co/u/pradeepjanga)\
**Post date:** [November 19, 2021, 1:31pm UTC](https://discuss.elastic.co/t/how-to-sort-resultview-data/288798/13 "2021-11-19T13:31:46Z")

</div>

This is effecting the relevance now. previously without adding the sort, the relevance data would be top but now it is not. is this because of the sort changes ?

---

<div class="post-metadata">

**Author:** ![JasonStoltz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasonstoltz/32/49893_2.png) [@JasonStoltz](https://discuss.elastic.co/u/JasonStoltz)\
**Post date:** [November 19, 2021, 2:06pm UTC](https://discuss.elastic.co/t/how-to-sort-resultview-data/288798/14 "2021-11-19T14:06:11Z")

</div>

If you are sorting your data study\_status\_order, then you are not using relevance. If you want to still use relevance but have certain values of study\_status\_order show up near the top, then use a value boost on that value: [Relevance Tuning Guide, Weights and Boosts | Elastic App Search Documentation [7.15] | Elastic ](https://www.elastic.co/guide/en/app-search/current/relevance-tuning-guide.html#relevance-tuning-guide-weights-and-boosts-boosts).

---

<div class="post-metadata">

**Author:** ![pradeepjanga](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@pradeepjanga](https://discuss.elastic.co/u/pradeepjanga)\
**Post date:** [November 22, 2021, 2:20pm UTC](https://discuss.elastic.co/t/how-to-sort-resultview-data/288798/15 "2021-11-22T14:20:34Z")

</div>

Can we use sort in the request body something like this ?

```auto
{

    "query": "cancer",

     "sort": [

    { "_score": "asc" },

    { "study_status_order": "asc" }

  ]

    

}

```

---

<div class="post-metadata">

**Author:** ![pradeepjanga](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@pradeepjanga](https://discuss.elastic.co/u/pradeepjanga)\
**Post date:** [November 22, 2021, 4:53pm UTC](https://discuss.elastic.co/t/how-to-sort-resultview-data/288798/16 "2021-11-22T16:53:52Z")

</div>

since i cannot use multiple sort in version 1.4, i switched to 1.8 and added below code but isn't working like i expect.

```auto
alwaysSearchOnInitialLoad: false,

    initialState: {

      sort: [

        { field: "study_status_order",

           direction:"asc"

        },

        { field: "_score",

           direction:"desc"

        }

      ]

    },

```

---

<div class="post-metadata">

**Author:** ![pradeepjanga](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@pradeepjanga](https://discuss.elastic.co/u/pradeepjanga)\
**Post date:** [November 22, 2021, 6:42pm UTC](https://discuss.elastic.co/t/how-to-sort-resultview-data/288798/17 "2021-11-22T18:42:00Z")

</div>

I see there is sortList field in the github code. Using that worked for me. is the github documentation didnt update ?

```auto
initialState: {

      sortList: [

        { field: "study_status_order",

           direction:"asc"

        },

        { field: "_score",

           direction:"desc"

        }

      ]

    },

```

| searchTerm | String | optional | Search terms to search for |
| --- | --- | --- | --- |
| sort | SortList | optional | List of fields and directions to sort on |
| sortDirection deprecated, use sort instead | String [asc | desc] | optional |
| sortField deprecated, use sort instead | String | optional | Name of field to sort on |

---

<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:** [December 20, 2021, 6:42pm UTC](https://discuss.elastic.co/t/how-to-sort-resultview-data/288798/18 "2021-12-20T18:42:59Z")

</div>

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