2

so here is my parse data: enter image description here

i am saving all the data in an NSMutableArray

i tried this :

var dataQuery:PFQuery = PFQuery(className: "Posts")
        dataQuery.orderByDescending("likes") // but this doesn't make sense at all.
        dataQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

i want the post at the top having maximum number of likes and so on. How can i do that? Thanks for your time..

1 Answer 1

3

This is not directly possible. But you can add a likesCount property an update it via cloud code on every save.

Parse.Cloud.beforeSave("Posts", function(request, response)
{
    var likes = request.object.has("likes") ? request.object.get("likes") : [];
    request.object.set("likesCount", likes.length);
    response.success();
});

Then use the likesCount property :

var dataQuery:PFQuery = PFQuery(className: "Posts")
dataQuery.orderByDescending("likesCount")
// ...
Sign up to request clarification or add additional context in comments.

2 Comments

where can i add this cloud code? :| i am a beginner,.
Nice answer, good idea and cool concise use of has and the ternary operator. Wish I could +2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.