# Array Sort Script to sort array by index

**URL:** https://discuss.elastic.co/t/array-sort-script-to-sort-array-by-index/312617
**Category:** Elasticsearch
**Tags:** painless
**Created:** [August 22, 2022, 11:38am UTC](https://discuss.elastic.co/t/array-sort-script-to-sort-array-by-index/312617 "2022-08-22T11:38:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Dakshay\_Agarwal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dakshay_agarwal/32/109930_2.png) [@Dakshay\_Agarwal](https://discuss.elastic.co/u/Dakshay_Agarwal)
#### Post date: [August 22, 2022, 11:38am UTC](https://discuss.elastic.co/t/array-sort-script-to-sort-array-by-index/312617/1 "2022-08-22T11:38:09Z")

</div>

```auto
PUT /my-index-000001/_doc/1?refresh
{
   "product": "chocolate",
   "salesManager" :["a","z","r"]
}

```

```auto
PUT /my-index-000001/_doc/2?refresh
{
   "product": "chocolate",
   "salesManager" :["v","f","d"]
}

```

```auto
PUT /my-index-000001/_doc/3?refresh
{
   "product": "chocolate",
   "salesManager" :["z","a","a"]
}

```

```auto
POST /my-index-000001/_search
{
   "sort": {
    "_script": {
      "type": "String",
      "script": {
        "lang": "painless",
        "source": "if(doc['salesManager'].size()>0) { return doc['salesManager'][0].toLowerCase()} return 'z';",
        "params": {
          "factor": 1.1
        }
      },
      "order": "asc"
    }
  }
}

```

I need to sort asc the index data using salesManager 0th index sort  
so the correct sort order is on doc id is 1-\>2-\>3

but I am getting 3-\>2-\>1

---

<div class="post-metadata">

### Author: ![Dakshay\_Agarwal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dakshay_agarwal/32/109930_2.png) [@Dakshay\_Agarwal](https://discuss.elastic.co/u/Dakshay_Agarwal)
#### Post date: [August 22, 2022, 12:14pm UTC](https://discuss.elastic.co/t/array-sort-script-to-sort-array-by-index/312617/2 "2022-08-22T12:14:54Z")

</div>

Accessing 0th Index in painless is giving the value after sorting that field.  
Why Array of Strings are getting sorted automatically ?

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [August 22, 2022, 12:50pm UTC](https://discuss.elastic.co/t/array-sort-script-to-sort-array-by-index/312617/3 "2022-08-22T12:50:04Z")

</div>

Hi @Dakshay_Agarwal

Maybe someone from the Elastic team can confirm but according to this [doc](https://www.elastic.co/guide/en/elasticsearch/guide/current/complex-core-fields.html#_multivalue_fields) there is an explanation for the behavior of your query.

---

<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: [September 19, 2022, 12:50pm UTC](https://discuss.elastic.co/t/array-sort-script-to-sort-array-by-index/312617/4 "2022-09-19T12:50:16Z")

</div>

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