2

Suppose I have 4 photos and I want to return a list sorted first on Rating desc, second on RatingsCount desc. Below cypher query gives me the correct result:

MATCH (n:`Photo`) WHERE n.RatingsCount > 0 RETURN n.Rating, n.RatingsCount ORDER BY n.Rating DESC, n.RatingsCount DESC LIMIT 20

Id 1-Rating 9-Ratingscount 1

Id 3-Rating 7-Ratingscount 2

Id 2-Rating 7-Ratingscount 1

Id 4-Rating 2-Ratingscount 1

Unfortunately I cannot translate it to Neo4jClient. Below query gives me a photolist ordered by Rating ascending and Ratingscount descending:

var query = await graphClient.Cypher
            .Match("(photo:Photo)")
            .Where((PhotoEntity photo) => photo.RatingsCount > 0);
            .ReturnDistinct((photo) => new
            {
                Photo = photo.As<PhotoEntity>()
            })
            .OrderByDescending("photo.Rating", "photo.RatingsCount")
            .Limit(20)
            .ResultsAsync;

Id 4-Rating 2-Ratingscount 1

Id 3-Rating 7-Ratingscount 2

Id 2-Rating 7-Ratingscount 1

Id 1-Rating 9-Ratingscount 1

If I change order of Rating and RatingsCount I get the photos in an even stranger order :) Any clue of what I am doing wrong here?

var query = await graphClient.Cypher
                .Match("(photo:Photo)")
                .Where((PhotoEntity photo) => photo.RatingsCount > 0);
                .ReturnDistinct((photo) => new
                {
                    Photo = photo.As<PhotoEntity>()
                })
                .OrderByDescending("photo.RatingsCount", "photo.Rating")
                .Limit(20)
                .ResultsAsync;

Id 1-Rating 9-Ratingscount 1

Id 2-Rating 7-Ratingscount 1

Id 4-Rating 2-Ratingscount 1

Id 3-Rating 7-Ratingscount 2

Jenny Pettersson
  • 241
  • 4
  • 17

1 Answers1

3

I took a look at Neo4jClient's source and OrderByDescending() simply string.join()s its arguments with commas and adds 'DESC' at the end. In other words: it sorts ASC on every property except for the last. I agree that this is counter intuitive so I tend to avoid it.

You can use OrderBy() instead:

OrderBy("photo.RatingsCount DESC, photo.Rating DESC")
Jan Van den bosch
  • 3,542
  • 3
  • 26
  • 38
  • Ah of course, next time I will check Neo4jClients's source before asking a question here :) I actually tried this approach but with lowercase desc instead of DESC which gave me an exception. In my case I want to sort on rating first but OrderBy("photo.Rating DESC, photo.RatingsCount DESC") works like a charm, thanks!! – Jenny Pettersson Sep 11 '14 at 05:27