# Sorting comblex Array elements with Painless script

**URL:** <https://discuss.elastic.co/t/sorting-comblex-array-elements-with-painless-script/118472>\
**Category:** Elasticsearch\
**Created:** [February 5, 2018, 2:25pm UTC](https://discuss.elastic.co/t/sorting-comblex-array-elements-with-painless-script/118472 "2018-02-05T14:25:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DBFBlackbull](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dbfblackbull/32/27372_2.png) [@DBFBlackbull](https://discuss.elastic.co/u/DBFBlackbull)\
**Post date:** [February 5, 2018, 2:25pm UTC](https://discuss.elastic.co/t/sorting-comblex-array-elements-with-painless-script/118472/1 "2018-02-05T14:25:17Z")

</div>

Hello!

I am looking for a way to use Update API with painless script to update the following record. I am using C# and NEST but writing the script as inline so the functionality should be the same.

```
{
    "name": "Boublegum"
    "variants": [
        {
            "variantId": "123456"
            "packageSize": 1
        }
    ]
}

```

The update should add a new variant to the list of variants without adding any duplicates. I have managed to do this using the script from this post: [Unique Array Elements in Painless Script](https://discuss.elastic.co/t/unique-array-elements-in-painless-script/66519)

```
{
	"script" : {
		"inline": "ctx._source.variants.addAll(params.variants); 
                   ctx._source.variants = ctx._source.variants.stream().distinct().collect(Collectors.toList());"
	}
}

```

However whenever I try to sort the variants aswell (by adding '.sorted()') I get the following error:

```
{
	"script" : {
		"inline": "ctx._source.variants.addAll(params.variants); 
                   ctx._source.variants = ctx._source.variants.stream().distinct().sorted().collect(Collectors.toList());"
	}
}

Type: class_cast_exception Reason: "java.util.HashMap cannot be cast to java.lang.Comparable"

```

I can guess the reason why this error is thrown. My C# class Variant has 2 properties, 'variantId' and 'packageSize' and this makes Elastic Search cast it to a java.util.HashMap with each property mapped in a name value pair.

I have tried the above script (with .sorted()) on a list of strings, which worked fine as java natively knows how to sort strings and primitive types.

**Now my question is: Is there anyway to sort the list of variants using any of the properties?**

I would like to sort according to packageSize. Is there anyway to use painless to sort these? I was thinking something along the lines of .sorted(v =\> v.packageSize()) or .sorted(v =\> v.GetPackageSize())

---

<div class="post-metadata">

**Author:** ![DBFBlackbull](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dbfblackbull/32/27372_2.png) [@DBFBlackbull](https://discuss.elastic.co/u/DBFBlackbull)\
**Post date:** [February 5, 2018, 2:46pm UTC](https://discuss.elastic.co/t/sorting-comblex-array-elements-with-painless-script/118472/2 "2018-02-05T14:46:00Z")

</div>

I found the answer myself after looking up how .sorted() works on a stream.  
The following code did the trick:

```
{
	"script" : {
		"inline": "ctx._source.name = params.name; 
                   ctx._source.variants.addAll(params.variants); 
                   ctx._source.variants = ctx._source.variants.stream().distinct()
                      .sorted((o1, o2) -> o1.get(\"packageSize\").compareTo(o2.get(\"packageSize\")))
                      .collect(Collectors.toList());"
	}
}
```

---

<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:** [March 5, 2018, 2:46pm UTC](https://discuss.elastic.co/t/sorting-comblex-array-elements-with-painless-script/118472/3 "2018-03-05T14:46:10Z")

</div>

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