首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tweet对象数组的ShellSort

tweet对象数组的ShellSort
EN

Stack Overflow用户
提问于 2019-01-10 00:52:03
回答 1查看 22关注 0票数 0

尝试根据用户的关注者数量对推文对象进行排序。数据和对象键是从mysql数据库自定义的,其中包含我为应用程序im构建收集的数据。我有一个名为dstore的数组,其中包含如下所示的各个对象:

代码语言:javascript
复制
{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;
}
EN

回答 1

Stack Overflow用户

发布于 2019-01-10 00:57:42

您可以像这样用sort简单地完成这项工作

代码语言:javascript
复制
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)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54114903

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档