# Compare elements in array

**URL:** <https://discuss.elastic.co/t/compare-elements-in-array/138928>\
**Category:** Kibana\
**Created:** [July 6, 2018, 1:45pm UTC](https://discuss.elastic.co/t/compare-elements-in-array/138928 "2018-07-06T13:45:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aleksa](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@aleksa](https://discuss.elastic.co/u/aleksa)\
**Post date:** [July 6, 2018, 1:45pm UTC](https://discuss.elastic.co/t/compare-elements-in-array/138928/1 "2018-07-06T13:45:26Z")

</div>

Hi,

I am trying to create new script field.

def pages = new ArrayList();  
if(doc['formular.pages'].length \> 1) {  
for(int i = 0; i \< doc['formular.pages'].length - 1; i++) {  
if(doc['formular.pages'][i] \> doc['formular.pages'][i + 1]) {  
pages.add(doc['formular.pages'][i]);  
}  
}  
}else{  
pages.add(0);  
}  
return pages;

It seems to be that it is not possible to access element in array using [i + 1].  
Any idea how i can resolve this issue?

---

<div class="post-metadata">

**Author:** ![Brandon\_Kobel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brandon_kobel/32/14829_2.png) [@Brandon\_Kobel](https://discuss.elastic.co/u/Brandon_Kobel)\
**Post date:** [July 9, 2018, 12:08pm UTC](https://discuss.elastic.co/t/compare-elements-in-array/138928/2 "2018-07-09T12:08:58Z")

</div>

Hey @aleksa what error are you getting when trying to use this scripted field? Also, which version of Kibana/Elasticsearch are you using?

---

<div class="post-metadata">

**Author:** ![aleksa](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@aleksa](https://discuss.elastic.co/u/aleksa)\
**Post date:** [July 11, 2018, 7:35am UTC](https://discuss.elastic.co/t/compare-elements-in-array/138928/3 "2018-07-11T07:35:15Z")

</div>

Hi,  
im using Version 6.2. There is no error message, however, when if statement (if(doc['formular.pages'][i] \> doc['formular.pages'][i + 1])) is true no new scripted field is added, only when the statement is false, a new field with 0 is added.

I also tried to rewrite this script:  
def pages = new ArrayList();  
def j;  
if(doc['formular.pages'].length \> 1) {  
for(int i = 0; i \< doc['formular.pages'].length - 1;) {  
j = i;  
++i;  
if(doc['formular.pages'][i] \< doc['formular.pages'][j]){  
pages.add(doc['formular.pages'][j]);  
}  
}  
}else{  
pages.add(0);  
}  
return pages;

It does not work as well.

---

<div class="post-metadata">

**Author:** ![Brandon\_Kobel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brandon_kobel/32/14829_2.png) [@Brandon\_Kobel](https://discuss.elastic.co/u/Brandon_Kobel)\
**Post date:** [July 11, 2018, 2:32pm UTC](https://discuss.elastic.co/t/compare-elements-in-array/138928/4 "2018-07-11T14:32:33Z")

</div>

@aleksa I believe that you're running into an issue with the way that Elasticsearch indexes and orders arrays. If you query your index for only the `formular.pages` you'll always see the pages always returned in order.

If you'd like to get the `formular.pages` in the original order when they were indexed, you can use the `params._source.formular.pages` to access this and use it throughout your script:

```auto
def output = new ArrayList();
def input = params._source.formular.pages;
if(input.length > 1) {
  for(int i = 0; i < input.length - 1; i++) {
    if(input[i] > input[i + 1]) {
      output.add(input[i]);
   }
  }
} else {
  output.add(0);
}

return output;

```

---

<div class="post-metadata">

**Author:** ![aleksa](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@aleksa](https://discuss.elastic.co/u/aleksa)\
**Post date:** [July 13, 2018, 8:11am UTC](https://discuss.elastic.co/t/compare-elements-in-array/138928/5 "2018-07-13T08:11:51Z")

</div>

Thank you. It works now.

---

<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:** [August 10, 2018, 8:12am UTC](https://discuss.elastic.co/t/compare-elements-in-array/138928/6 "2018-08-10T08:12:24Z")

</div>

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