# Ordering by nested field

**URL:** <https://discuss.dgraph.io/t/ordering-by-nested-field/12613>\
**Category:** GraphQL\
**Tags:** kind:question, status:accepted, kind:feature, ticket:created\
**Created:** [February 2, 2021, 8:57pm UTC](https://discuss.dgraph.io/t/ordering-by-nested-field/12613 "2021-02-02T20:57:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthias\_Baetens](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/matthias_baetens/32/6041_2.png) [@Matthias\_Baetens](https://discuss.dgraph.io/u/Matthias_Baetens)\
**Post date:** [February 2, 2021, 8:57pm UTC](https://discuss.dgraph.io/t/ordering-by-nested-field/12613/1 "2021-02-02T20:57:49Z")

</div>

## I Want to Do

I want to order my results by a nested field (field from another node)

## What I Did

I extended the Task example, adding a date to the schema:

```auto
type Task {
  id: ID!
  title: String!
  completed: Boolean!
  user: User!
  date: ReportedDate @hasInverse(field: task)
}

type ReportedDate {
  date: DateTime
  task: Task
}

type User {
  username: String! @id
  name: String
  tasks: [Task] @hasInverse(field: user)
}

```

Now I want to retrieve all tasks per user, sorted by their respective dates using GraphQL on top of Dgraph.

---

<div class="post-metadata">

**Author:** ![rajas](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/rajas/32/4393_2.png) [@rajas](https://discuss.dgraph.io/u/rajas)\
**Post date:** [February 3, 2021, 10:43am UTC](https://discuss.dgraph.io/t/ordering-by-nested-field/12613/2 "2021-02-03T10:43:31Z")

</div>

Sorting using a nested field is currently not supported.

As a possible workaround, you may include `dateTime: DateTime` field directly inside `type Task` .  
If that is done, the following query will be able to fetch tasks sorted according to Date for the User.

```auto
query{
  queryUser{
    name
    tasks(order:{asc:dateTime}){
      title
    }
  }
}

```

I am trying to understand your use case here. Is there any specific reason that `ReportedDate` has been as a separate `type` rather than having `dateTime: DateTime` field inside `type Task`.

If your use case is fairly common and there is no other way to do this, we can accept this as a feature request. It will then be be worked upon along with [supporting nested filters](http://discuss.hypermode.com/t/proposal-nested-object-filters-for-graphql-rewritten-as-var-blocks-in-dql/12252).

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [February 3, 2021, 11:46am UTC](https://discuss.dgraph.io/t/ordering-by-nested-field/12613/3 "2021-02-03T11:46:12Z")

</div>

I have many use cases for this. One being sorting addresses by state or city where state and city are their own types. They are their own types to allow to do a quick selection of all cities or all states for building out a selection list in the GUI. We also have census data and geo data attached to the city and states. That is just one example.

---

<div class="post-metadata">

**Author:** ![rajas](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/rajas/32/4393_2.png) [@rajas](https://discuss.dgraph.io/u/rajas)\
**Post date:** [February 3, 2021, 11:51am UTC](https://discuss.dgraph.io/t/ordering-by-nested-field/12613/4 "2021-02-03T11:51:40Z")

</div>

Thanks for your quick reply @amaster507 .

Yeah, this feels like a valid use case. I have accepted this as a feature request and will be worked upon (after 21.03).

---

<div class="post-metadata">

**Author:** ![Matthias\_Baetens](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/matthias_baetens/32/6041_2.png) [@Matthias\_Baetens](https://discuss.dgraph.io/u/Matthias_Baetens)\
**Post date:** [February 3, 2021, 5:36pm UTC](https://discuss.dgraph.io/t/ordering-by-nested-field/12613/6 "2021-02-03T17:36:25Z")

</div>

Awesome @rajas!
