# How to keep list values from same Group next to each other no matter the Sort Order?

**URL:** <https://discuss.elastic.co/t/how-to-keep-list-values-from-same-group-next-to-each-other-no-matter-the-sort-order/189158>\
**Category:** Elasticsearch\
**Created:** [July 5, 2019, 6:59pm UTC](https://discuss.elastic.co/t/how-to-keep-list-values-from-same-group-next-to-each-other-no-matter-the-sort-order/189158 "2019-07-05T18:59:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fgravel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fgravel/32/46093_2.png) [@fgravel](https://discuss.elastic.co/u/fgravel)\
**Post date:** [July 5, 2019, 6:59pm UTC](https://discuss.elastic.co/t/how-to-keep-list-values-from-same-group-next-to-each-other-no-matter-the-sort-order/189158/1 "2019-07-05T18:59:33Z")

</div>

I'm using [ElasticSearch.net](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/index.html) to display a list of Flight Tickets for Clients. Some of theses Clients are **grouped** together when we process the flight tickets. I want my list to display theses groups together no matter the sorting order.

I've tried to aggregate on the GroupId field but figured that this is not what I want. I believe aggregate is similar to _Group By_ in _SQL_ ?

_The commented code here is what I tried:_

```auto
var response = await _elastic.SearchAsync<FlightTicketRequestDocument>(search => search
                .Index(IndexNames.FlightTicketRequests)
                .AllTypes()
                .Query(q => q.Bool(b => b.Must(m => m.QueryString(searchQuery.Query, Fields()))
                .Filter(Filters(searchQuery))))
                //.Aggregations(agg => agg.Terms("group_by_groupId", g => g.Field(f => f.GroupId)))
                .Sort(p => searchQuery.Sort != null ? p.Field(searchQuery.Sort.Member, searchQuery.Sort.SortDirection) : p)
                .Page(searchQuery.Page, searchQuery.PageSize), cancellationToken);

```

To be clear: I want to see all clients of a group next to each others no matter the sorting order of the table but I still want my sorting to work independently from GroupId

---

<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 2, 2019, 6:59pm UTC](https://discuss.elastic.co/t/how-to-keep-list-values-from-same-group-next-to-each-other-no-matter-the-sort-order/189158/2 "2019-08-02T18:59:33Z")

</div>

Closed on August 2, 2019, 6:59pm UTC

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