# Sort results by "is item in array"

**URL:** https://discuss.elastic.co/t/sort-results-by-is-item-in-array/129278
**Category:** Elasticsearch
**Created:** [April 24, 2018, 9:52am UTC](https://discuss.elastic.co/t/sort-results-by-is-item-in-array/129278 "2018-04-24T09:52:17Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![mattfletcher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattfletcher/32/30396_2.png) [@mattfletcher](https://discuss.elastic.co/u/mattfletcher)
#### Post date: [April 24, 2018, 9:52am UTC](https://discuss.elastic.co/t/sort-results-by-is-item-in-array/129278/1 "2018-04-24T09:52:17Z")

</div>

I have a data set which includes within each document an array of user IDs that have marked the document as a "preferred" one. I want to be able to sort my documents such that these preferred documents are shown first, before the other documents.

So, how can I sort a set of results by "array contains X", where X is a given user ID? I still want to include all results, but sort the ones containing a matching user ID to the top.

---

<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: [May 22, 2018, 9:52am UTC](https://discuss.elastic.co/t/sort-results-by-is-item-in-array/129278/2 "2018-05-22T09:52:18Z")

</div>

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