我在我正在构建的网站中的Parse.com Javascript SDK中有这个简单的例子.
课程如下
_User结构:
帖子结构:
> objectId
> postName – 字符串
> postMsg – 字符串
> postAuthor – 指针< _用户> (指向用户类的指针)
评论结构:
> objectId
> msg – 字符串
> post – Pointer<帖子> (指向帖子类的指针)
> user – 指针< _用户> (指向用户类的指针)
我将此查询发送到评论类,以获取所有评论以及作者姓名和其他一些内容.
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
}
});
}
})
所以我的问题是,在这个查询中,如何使结果的顺序由POSTS类中的“createdAt”降序,而不是主查询的“注释类”?
有没有办法做到这一点?
就像是
query.descending("post.createdAt");
任何的想法?
最佳答案
编辑:只需使用array.sort对结果进行排序
results = result.sort(function (a,b){
// decide with result is greater
// return + for stay for a > b
// return negative for b > a
})
旧答案:
摘自https://www.parse.com/docs/js/api/classes/Parse.Query.html:
Parse.Query addDescending(key)
按给定键按降序对结果进行排序,但也可以添加辅助排序描述符而不覆盖_order.
参数:
key<(String | String | ... String>
order by的键,是一串逗号分隔值,或一组键或多个键.
Parse.Query降序(键)
按给定键按降序对结果进行排序.
参数:
key<(String | String | ... String>
order by的键,因此您可以链接此调用.
query.include("post.createdAt")
query.descending("post.createdAt")