Sort query results by any edge property

What I want to do

Hello everyone,

Weird genres in this context are the ones that have only one movie.

Get all the weird genres ordered by the number of genres of the movie.

{
  weirdGenres(func: eq(count(~genre), 1), orderdesc: val(numGenres)) @cascade{
    genre_name: name@en
  	movie: ~genre  {
        name: name@en
    	numGenres as count(genre)
    }
  }
}

This is not getting my weird genres sorted by the number of genres of the film.

What I did

I used a support function (max, avg, sum) to move the variable one level up and be able to sort.

{
  wierdGenres as var(func: eq(count(~genre), 1)) @cascade {
    ~genre {
    	numGenres as count(genre)
  	}
  	temp as max(val(numGenres))
  }
  ordered(func: uid(wierdGenres), orderdesc: val(temp)) @cascade {
    weirdGenre:name@en
  	movie:~genre {
    	name:name@en
    	genres: val(numGenres)
    }
  }
}

Expected output

"ordered": [
      {
        "weirdGenre": "Shōjo manga",
        "movie": [
          {
            "name": "Adolescence of Utena",
            "genres": 15
          }
        ]
      },
      {
        "weirdGenre": "C-Movie",
        "movie": [
          {
            "name": "Ken Russell's Fall of the Louse of Usher",
            "genres": 11
          }
        ]
      },
      {
        "weirdGenre": "Erotic Science Fiction",
        "movie": [
          {
            "name": "Flesh Gordon",
            "genres": 11
          }
        ]
      },
      {
        "weirdGenre": "Edupunk",
        "movie": [
          {
            "name": "Internet Rising",
            "genres": 8
          }
        ]
      },
   ]

Dgraph metadata

dgraph version

v20.11.0

That’s the only way to do it in Dgraph. There are always different ways, but you have always a combination of multiple blocks. You can’t gather values from a variable from its own block.

1 Like

Thanks @MichelDiz for the promptly response!, I appreciate it. Have a good one.