# Ordering via val variable doesn't seem to work

**URL:** <https://discuss.dgraph.io/t/ordering-via-val-variable-doesnt-seem-to-work/8330>\
**Category:** Dgraph\
**Created:** [July 12, 2020, 6:37pm UTC](https://discuss.dgraph.io/t/ordering-via-val-variable-doesnt-seem-to-work/8330 "2020-07-12T18:37:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anurag](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anurag/32/2599_2.png) [@Anurag](https://discuss.dgraph.io/u/Anurag)\
**Post date:** [July 12, 2020, 6:37pm UTC](https://discuss.dgraph.io/t/ordering-via-val-variable-doesnt-seem-to-work/8330/1 "2020-07-12T18:37:20Z")

</div>

Schema:

```auto
    name: string @index(hash).
    nickname: string @index(hash).
    age: string .
    class: string @index(exact, trigram) .
    type TypeName {
        name: string
        class: string
        nickname: string
        age: int
    }

```

Data:

```auto
        <1> <name> "Anurag" .
        <1> <class> "first" .
        <1> <nickname> "Anu" .
        <1> <age> "10" .
        <1> <dgraph.type> "TypeName" .

        <2> <name> "Brad" .
        <2> <class> "second" .
        <2> <nickname> "BD" .
        <2> <age> "20" .
        <2> <dgraph.type> "TypeName" .

```

Query:

```auto
    q1(func: has(name)) {
        v as name
        a as age
    }
    q2(func: has(name), orderdesc: val(v)) { //Works if I pass orderdesc: name
        name
        age
        val(v)
        val(a)
    }

```

Output:

```auto
{
  "q1": [
    {
      "age": "10", 
      "name": "Anurag"
    }, 
    {
      "age": "20", 
      "name": "Brad"
    }
  ], 
  "q2": []
}

```

Is there something I am missing, I don’t know why `q2` doesn’t seem to work?

---

<div class="post-metadata">

**Author:** ![Anurag](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anurag/32/2599_2.png) [@Anurag](https://discuss.dgraph.io/u/Anurag)\
**Post date:** [July 12, 2020, 6:44pm UTC](https://discuss.dgraph.io/t/ordering-via-val-variable-doesnt-seem-to-work/8330/2 "2020-07-12T18:44:23Z")

</div>

I feel this is a bug and not a feature we don’t support because I did see my matrix getting correctly ordered uptil [here](https://github.com/dgraph-io/dgraph/blob/8d80aed88b4fa98f001ceb77d2fd4637cd22c9df/query/query.go#L2492)

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [July 12, 2020, 7:03pm UTC](https://discuss.dgraph.io/t/ordering-via-val-variable-doesnt-seem-to-work/8330/3 "2020-07-12T19:03:51Z")

</div>

You have to do it via multiple blocks  
e.g

```auto
{    
  G as q1(func: has(name)) {
        v as name
        a as age
    }
    q2(func: uid(G), orderdesc: val(v)) {
        name
        age
        val(v)
        val(a)
    }
}

```

Without the G reference (that works as a “map”) it won’t apply anything in the query block. The variables are based on levels and a kind of uid mapping.

This avoids things like

```auto
{    
   q1(func: has(name)) {
        v as name
        a as age
    }
    q2(func: has(title), orderdesc: val(v)) { 
        name
        age
        val(v)
        val(a)
    }
}

```

This query above would break anyway. Cuz it doesn’t has references.

---

<div class="post-metadata">

**Author:** ![Anurag](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anurag/32/2599_2.png) [@Anurag](https://discuss.dgraph.io/u/Anurag)\
**Post date:** [July 12, 2020, 7:04pm UTC](https://discuss.dgraph.io/t/ordering-via-val-variable-doesnt-seem-to-work/8330/4 "2020-07-12T19:04:27Z")

</div>

It would work if I modify my query to be:

```auto
    f as q1(func: has(name)) {
        v as name
        a as age
    }
    q2(func: uid(f), orderdesc: val(v)) { //Works if I pass orderdesc: name
        name
        age
        val(v)
        val(a)
    }

```

Weird…

---

<div class="post-metadata">

**Author:** ![Anurag](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anurag/32/2599_2.png) [@Anurag](https://discuss.dgraph.io/u/Anurag)\
**Post date:** [July 12, 2020, 7:07pm UTC](https://discuss.dgraph.io/t/ordering-via-val-variable-doesnt-seem-to-work/8330/5 "2020-07-12T19:07:32Z")

</div>

> [@MichelDiz](#):
>
> This query above would break anyway. Cuz it doesn’t has references.

You mean the above query would break as well because it doesn’t have a map between two queries. Also, we do not want to order results of one query via variables defined in an entirely different query.

Is that correct understanding?

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [July 12, 2020, 7:10pm UTC](https://discuss.dgraph.io/t/ordering-via-val-variable-doesnt-seem-to-work/8330/6 "2020-07-12T19:10:37Z")

</div>

> [@Anurag](#):
>
> You mean the above query would break as well because it doesn’t have a map between two queries.

Yes, and potentially, the second block (using has) would bring more other new nodes. That would break the query. The right way is by “Mapping” it.

> [@Anurag](#):
>
> Also, we do not want to order results of one query via variables defined in an entirely different query.

yep
