# Sort by every element of array

**URL:** <https://discuss.elastic.co/t/sort-by-every-element-of-array/262201>\
**Category:** Elasticsearch\
**Created:** [January 26, 2021, 8:02am UTC](https://discuss.elastic.co/t/sort-by-every-element-of-array/262201 "2021-01-26T08:02:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dmitrii](https://avatars.discourse-cdn.com/v4/letter/d/9fc29f/32.png) [@Dmitrii](https://discuss.elastic.co/u/Dmitrii)\
**Post date:** [January 26, 2021, 8:02am UTC](https://discuss.elastic.co/t/sort-by-every-element-of-array/262201/1 "2021-01-26T08:02:50Z")

</div>

Hello,

I have some problems with sorting by element of array.  
If I have for example 2 documents like:

```auto
{
  "_id" : "1",
  "_source" : {
    "users" : [{"name" : "John"},{"name" : "Paul"}]
  }
}

{
  "_id" : "2",
  "_source" : {
    "users" : [{"name" : "Alice"},{"name" : "John"}]
  }
}

```

Can you please tell me if I have the ability to sort and group by each element of the array using pagination and find one document several times?

The result that is to be expected after sort:

```auto
    {
      "hits" : {
        "hits" : [
          {
            "_id" : "2",
            "sort" : ["Alice"]
          },
          {
            "_id" : "1",
            "sort" : ["John"]
          },
    	  {
            "_id" : "2",
            "sort" : ["John"]
          },
    	  {
            "_id" : "1",
            "sort" : ["Paul"]
          }
        ]
      }
    }

```

I've tried doing this via sorting or aggregation.

For aggregation, you can group elements, but end-to-end sorting and pagination do not work for me.

---

<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:** [February 23, 2021, 8:03am UTC](https://discuss.elastic.co/t/sort-by-every-element-of-array/262201/2 "2021-02-23T08:03:02Z")

</div>

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