# Painless does not return array elements in the same order they were inserted

**URL:** https://discuss.elastic.co/t/painless-does-not-return-array-elements-in-the-same-order-they-were-inserted/84957
**Category:** Elasticsearch
**Created:** [May 8, 2017, 4:14pm UTC](https://discuss.elastic.co/t/painless-does-not-return-array-elements-in-the-same-order-they-were-inserted/84957 "2017-05-08T16:14:27Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Zaid\_Amir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zaid_amir/32/54679_2.png) [@Zaid\_Amir](https://discuss.elastic.co/u/Zaid_Amir)
#### Post date: [May 8, 2017, 4:14pm UTC](https://discuss.elastic.co/t/painless-does-not-return-array-elements-in-the-same-order-they-were-inserted/84957/1 "2017-05-08T16:14:27Z")

</div>

Hi,

I am trying to write a script to calculate the euclidean distance for some points stored as an array. Each array consists of 128 Double values.

The field is currently indexed and stored as such:

`"my_array": [0.176, 0.165, 0.987, 0.67, ...]`

When I use Painless to read and loop the array using `doc['my_array'].values`; it returns the array sorted for some reason. So instead of the above I get the array as:`[0.165, 0.176, 0.67, 0.987, ...]`

What other options do I have to get the array in the same order?

I am using ES 5.2

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [May 9, 2017, 9:39am UTC](https://discuss.elastic.co/t/painless-does-not-return-array-elements-in-the-same-order-they-were-inserted/84957/2 "2017-05-09T09:39:57Z")

</div>

Values are indeed always returned in order. If you need to maintain order, one potential trick is to encode your values in such a way that they will be in order, for instance if all values are between 0 and 1, you could add the index to all values at index time, ie. `[0.176, 0.165, 0.987, 0.67, ...]` becomes `[0.176, 1.165, 2.987, 3.67, ...]` and then subtract it at search time.

---

<div class="post-metadata">

### Author: ![Zaid\_Amir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zaid_amir/32/54679_2.png) [@Zaid\_Amir](https://discuss.elastic.co/u/Zaid_Amir)
#### Post date: [May 10, 2017, 11:33am UTC](https://discuss.elastic.co/t/painless-does-not-return-array-elements-in-the-same-order-they-were-inserted/84957/3 "2017-05-10T11:33:30Z")

</div>

Yeah, problem is my values are between -1 and 1 so for now I have mapped the array as keyword then splitting and parsing each double entry

---

<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: [June 7, 2017, 11:40am UTC](https://discuss.elastic.co/t/painless-does-not-return-array-elements-in-the-same-order-they-were-inserted/84957/4 "2017-06-07T11:40:03Z")

</div>

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