Jenny Pettersson
Jenny Pettersson

Reputation: 261

Orderby desc multiple properties in Neo4jClient

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

Upvotes: 2

Views: 3047

Answers (1)

Bossie
Bossie

Reputation: 3612

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")

Upvotes: 3

Related Questions