# Sorting an array of objects

**URL:** https://discuss.elastic.co/t/sorting-an-array-of-objects/137453
**Category:** Elasticsearch
**Created:** [June 26, 2018, 2:37pm UTC](https://discuss.elastic.co/t/sorting-an-array-of-objects/137453 "2018-06-26T14:37:08Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Aymen\_Fezai](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@Aymen\_Fezai](https://discuss.elastic.co/u/Aymen_Fezai)
#### Post date: [June 26, 2018, 2:37pm UTC](https://discuss.elastic.co/t/sorting-an-array-of-objects/137453/1 "2018-06-26T14:37:08Z")

</div>

I am new to elasticsearch.  
The documents I got look like this :

```
	user_name : "test", 
piInfo: {
	profile: {
		word_count: 535,
		word_count_message: "There were 535 words in the input.",
		processed_language: "en",
		personality: [
			{
				name: "Openness",
				category: "personality",
				percentile: 0.0015293409544490655,
				children: []
			},
			{
				name: "Conscientiousness",
				category: "personality",
				percentile: 0.6803430984001135,
				children: []
			}
		]
	}
}

```

I want to sort users (user\_name) by personality (for example : "Conscientiousness.percentile")

---

<div class="post-metadata">

### Author: ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)
#### Post date: [June 26, 2018, 7:17pm UTC](https://discuss.elastic.co/t/sorting-an-array-of-objects/137453/2 "2018-06-26T19:17:16Z")

</div>

If you want to keep that data structure, you'll need to map the `"personality"` field as a `nested` data type: [https://www.elastic.co/guide/en/elasticsearch/reference/6.3/nested.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/nested.html)

Then you can sort on it using the `nested sort`: [https://www.elastic.co/guide/en/elasticsearch/reference/6.3/search-request-sort.html#nested-sorting](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/search-request-sort.html#nested-sorting)

---

<div class="post-metadata">

### Author: ![Aymen\_Fezai](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@Aymen\_Fezai](https://discuss.elastic.co/u/Aymen_Fezai)
#### Post date: [June 27, 2018, 9:31am UTC](https://discuss.elastic.co/t/sorting-an-array-of-objects/137453/3 "2018-06-27T09:31:16Z")

</div>

This what I came up with so far:

```
"query": {
            "nested": {
              "path": "piInfo.profile.personality",
              "query": {
                "bool": {
                  "must":
                    { "match": { "piInfo.profile.personality.name": "Openness" }} 
                }
              }
          }
        },
        "sort" : {
            "piInfo.profile.personality.percentile" : {
                "order" : "asc",
                "nested": {
                    "path": "piInfo.profile.personality",
                    "filter": {
                    "match": { "piInfo.profile.personality.name": "Openness" }
                    }
                }
            }
        }

```

And I got this error:

```
[nested] nested object under path [piInfo.profile.personality] is not of nested type

```

And that is logic because I didn't mapp it. I am taking data from an API and I am storing it as it is.

Is there a way around that?

---

<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: [July 25, 2018, 9:42am UTC](https://discuss.elastic.co/t/sorting-an-array-of-objects/137453/4 "2018-07-25T09:42:02Z")

</div>

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