Konstantinos Natsios
Konstantinos Natsios

Reputation: 2924

Set query descending order from another class in Parse.com

I have this simple example in Parse.com Javascript SDK in the site that i'm building.

The Classes are the following

_User Structure:

Posts Structure:

Comments Structure:

And i make this query to the Comments Class to take all the comments and also the Author's name and some other stuff.

var Comments= Parse.Object.extend("Comments");
var query = new Parse.Query(Comments);
/*Get the Post's Info*/
query.include("post");
/*Get the Post Author's Info*/
query.include("post.postAuthor");
/*Get the Comment's Author Info*/
query.include("user");
query.find().then(function(results){
    /* Go Through Each Comment*/
    var commentsArray = new Array();
    for(i in results){
        /* Set obj to current comment*/
        var obj = results[i];
        /* Get Post's Name */
        var postName = obj.get("post").get("postName");
        /* Get Post's Message */
        var postMsg = obj.get("post").get("postMsg");
        /* Get Post Author's Name */
        var authorName = obj.get("post").get("postAuthor").get("name");
        /* Get Comment's Message */
        var commentMsg = obj.get("msg");
        /* Get Comment's Author*/
        var commentAuthor = obj.get("user").get("name");

        /* Let's Put the Comment Information in an Array as an Object*/
        commentsArray.push({
            post:{
                name: postName,
                msg: postMsg
            },
            author: authorName,
            comment: {
                author: commentAuthor,
                msg: commentMsg
            }
        });
    }
})

So my question is, in this query how can i make the order of the results to be descending by "createdAt" from POSTS Class, and not from the Comments Class that the main query is?

Is there any way to do that? Something like

query.descending("post.createdAt");

Any idea?

Upvotes: 5

Views: 1908

Answers (1)

user6101582
user6101582

Reputation:

Edit: Just sort the results with array.sort

results = result.sort(function (a, b){
    // decide with result is greater
    // return + for stay for a > b
    // return negative for b > a
})

Old Answer: Taken from https://www.parse.com/docs/js/api/classes/Parse.Query.html:

Parse.Query addDescending( key ) Sorts the results in descending order by the given key, but can also add secondary sort descriptors without overwriting _order.

Parameters: key <(String | String | ...String> The key to order by, which is a string of comma separated values, or an Array of keys, or multiple keys.

Returns: Returns the query, so you can chain this call.

Parse.Query descending( key ) Sorts the results in descending order by the given key.

Parameters: key <(String | String | ...String> The key to order by, which is a string of comma separated values, or an Array of keys, or multiple keys.

Returns: Returns the query, so you can chain this call.

query.include("post.createdAt")
query.descending("post.createdAt")

Upvotes: 3

Related Questions