# Customized sort in ElasticSearch

**URL:** <https://discuss.elastic.co/t/customized-sort-in-elasticsearch/317198>\
**Category:** Elasticsearch\
**Created:** [October 21, 2022, 12:46pm UTC](https://discuss.elastic.co/t/customized-sort-in-elasticsearch/317198 "2022-10-21T12:46:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Apurba](https://avatars.discourse-cdn.com/v4/letter/a/edb3f5/32.png) [@Apurba](https://discuss.elastic.co/u/Apurba)\
**Post date:** [October 21, 2022, 12:46pm UTC](https://discuss.elastic.co/t/customized-sort-in-elasticsearch/317198/1 "2022-10-21T12:46:41Z")

</div>

Can I use custom sort in ES?  
Like, in my ES data, one field values are -\> "A", "A", "B", "B", "C", "C", "C", "D"  
Can I sort this like -\> "D", "B", "B", "C", "C", "C", "A", "A"

If we can sort like this, how the query structure will be looked like?

---

<div class="post-metadata">

**Author:** ![Apurba](https://avatars.discourse-cdn.com/v4/letter/a/edb3f5/32.png) [@Apurba](https://discuss.elastic.co/u/Apurba)\
**Post date:** [October 25, 2022, 4:50am UTC](https://discuss.elastic.co/t/customized-sort-in-elasticsearch/317198/2 "2022-10-25T04:50:19Z")

</div>

If anyone aware about it, can you please help me here?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [October 25, 2022, 6:25am UTC](https://discuss.elastic.co/t/customized-sort-in-elasticsearch/317198/3 "2022-10-25T06:25:29Z")

</div>

Have you looked at using [scripted sort](https://www.elastic.co/guide/en/elasticsearch/painless/8.4/painless-sort-context.html) to potentially achieve this? I am not sure it is possible but it is the only way I can see to get that type of result.

---

<div class="post-metadata">

**Author:** ![Apurba](https://avatars.discourse-cdn.com/v4/letter/a/edb3f5/32.png) [@Apurba](https://discuss.elastic.co/u/Apurba)\
**Post date:** [October 26, 2022, 6:42am UTC](https://discuss.elastic.co/t/customized-sort-in-elasticsearch/317198/4 "2022-10-26T06:42:42Z")

</div>

Hi, @Christian_Dahlqvist, thanks for the reply. Actually I was not aware of that thing. Let me see.

---

<div class="post-metadata">

**Author:** ![Apurba](https://avatars.discourse-cdn.com/v4/letter/a/edb3f5/32.png) [@Apurba](https://discuss.elastic.co/u/Apurba)\
**Post date:** [October 28, 2022, 4:48am UTC](https://discuss.elastic.co/t/customized-sort-in-elasticsearch/317198/5 "2022-10-28T04:48:30Z")

</div>

Hi @Christian_Dahlqvist I was playing around the scripted sort and painless API but unable to achieve the said scenario.  
Is there any other way we can do that?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [October 28, 2022, 5:02am UTC](https://discuss.elastic.co/t/customized-sort-in-elasticsearch/317198/6 "2022-10-28T05:02:57Z")

</div>

Not that I can think of.

---

<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:** [November 25, 2022, 5:03am UTC](https://discuss.elastic.co/t/customized-sort-in-elasticsearch/317198/7 "2022-11-25T05:03:29Z")

</div>

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