尝试根据用户的关注者数量对推文对象进行排序。数据和对象键是从mysql数据库自定义的,其中包含我为应用程序im构建收集的数据。我有一个名为dstore的数组,其中包含如下所示的各个对象:
{tweet_text: "so I'm a bit surprised that people are... surprised. that the "action plan" to cool Toronto housing is just...meetings."
user_FollowersCount: "1415"
user_ProfileImageUrl: "http://pbs.twimg.com/profile_images/821936571842707456/gPPRWsyS_normal.jpg"
user_ScreenName: "RosalindR"}
public shellsrt(dstore){
var gaps = [20,10,1];
for (var g = 0; g < gaps.length; ++g) {
for(var i = gaps[g]; i < dstore.length; ++i)
{
var temp = dstore[i];
var followcount = parseInt(temp['user_FollowersCount']);
var j = i;
if(dstore[j - gaps[g]] !== undefined){
var gapdiff = parseInt(dstore[j - gaps[g]]['user_FollowersCount']);
while( j >= gaps[g] && gapdiff > followcount )
{
if ( dstore[gapdiff] != undefined) {
dstore[j] = dstore[gapdiff];
}
j -= gaps[g];
}
}
dstore[j] = temp;
}
}
console.log(dstore);
//return dstore;
}发布于 2019-01-10 00:57:42
您可以像这样用sort简单地完成这项工作
let arr = [{tweet_text: "so I'm that the ",action_plan :" to cool Toronto housing is just...meetings.",user_FollowersCount:"1415",user_ProfileImageUrl: "http://pbs.twimg.com/profile_images/821936571842707456/gPPRWsyS_normal.jpg",user_ScreenName: "RosalindR"},
{tweet_text: "so I'm that the ",action_plan :" to cool Toronto housing isjust...meetings.",user_FollowersCount: "1485",user_ProfileImageUrl: "http://pbs.twimg.com/profile_images/821936571842707456/gPPRWsyS_normal.jpg",user_ScreenName: "RosalindR"},
{tweet_text: "so I'm that the ",action_plan :" to cool Toronto housing isjust...meetings.",user_FollowersCount:"115",user_ProfileImageUrl: "http://pbs.twimg.com/profile_images/821936571842707456/gPPRWsyS_normal.jpg",user_ScreenName: "RosalindR"}]
let op = arr.sort((a,b)=> a.user_FollowersCount - b.user_FollowersCount)
console.log(op)
https://stackoverflow.com/questions/54114903
复制相似问题