# Custom sorting

**URL:** <https://discuss.elastic.co/t/custom-sorting/345525>\
**Category:** Elasticsearch\
**Created:** [October 22, 2023, 4:06pm UTC](https://discuss.elastic.co/t/custom-sorting/345525 "2023-10-22T16:06:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxim-pushchinskiy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maxim-pushchinskiy/32/110199_2.png) [@maxim-pushchinskiy](https://discuss.elastic.co/u/maxim-pushchinskiy)\
**Post date:** [October 22, 2023, 4:06pm UTC](https://discuss.elastic.co/t/custom-sorting/345525/1 "2023-10-22T16:06:16Z")

</div>

I have documents like:

```auto
POST /your-index-name/_doc/1
{
  "bbCategories": ["Shirts"],
  "otherField": "value1"
}

POST /your-index-name/_doc/2
{
  "bbCategories": ["Trousers"],
  "otherField": "value2"
}

POST /your-index-name/_doc/3
{
  "bbCategories": ["Dresses"],
  "otherField": "value3"
}
POST /your-index-name/_doc/4
{
  "bbCategories": ["Shirts"],
  "otherField": "value4"
}

POST /your-index-name/_doc/5
{
  "bbCategories": ["Shirts"],
  "otherField": "value5"
}
POST /your-index-name/_doc/6
{
  "bbCategories": ["Trousers"],
  "otherField": "value6"
}

POST /your-index-name/_doc/7
{
  "bbCategories": ["Trousers"],
  "otherField": "value7"
}
POST /your-index-name/_doc/8
{
  "bbCategories": ["Dresses"],
  "otherField": "value8"
}

POST /your-index-name/_doc/9
{
  "bbCategories": ["Dresses"],
  "otherField": "value9"
}

```

I want to sort my elastic results by field 'bbCategories', but in specific way.

The results should have next order:  
`Shirts1, Trousers1, Dresses1, Shirts2, Trousers2, Dresses2, Shirts3, Trousers3, Dresses3.`

The question: is it possible to achieve? I tried to create sort script, but seams that elastic can't save the conditions.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 22, 2023, 4:52pm UTC](https://discuss.elastic.co/t/custom-sorting/345525/2 "2023-10-22T16:52:26Z")

</div>

What did you try? And how the order should be computed? What is the exact rule?

---

<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 19, 2023, 4:53pm UTC](https://discuss.elastic.co/t/custom-sorting/345525/3 "2023-11-19T16:53:14Z")

</div>

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