如何把后台返回数据的根据某个选项去重新排序?

比如:一个用户列表要按照用户的积分高低去排序渲染(一般后台会排序之后返回给你);

例如下面的data是请求回来的数据,

var data={
      "ret_code": "0",
      "ret_msg": "ok",
      "data": {
        "users": [
              {
            "pfid": 1000011,
            "nickname": "cabbageancy",
            "headimg": "http://wx.qlogo.cn/mmopen/ajNVdqHZLLASXYCVQQeiaXKA07Mx1Lu24juBVZ40ib9IsI6ImibfsWD1R3JS5I5L4LZMmgxpC4B7Y1taCZmXHQVKic4lbHibcntI7L6S7K4gnMW8/0",
            "count": 0,
            "follow_status": 0,
            "score": 19,
            "live_id":"454654646"
          },
          {
            "pfid": 1000004,
            "nickname": "减肥沟沟壑壑健健康康快快",
            "headimg": "http://playback-langlive.ufile.ucloud.cn/4825891161c4d9a377be0785078c2158.jpg",
            "count": 0,
            "follow_status": 0,
            "score": 17,
            "live_id":"454654646"
          },
          {
            "pfid": 1023841,
            "nickname": "优雅的猫",
            "headimg": "http://blob.ufile.ucloud.com.cn/e101f4896dbd4de38b7d7e7123828ee608d7b97251148720bb7678bf10ea21b8",
            "count": 0,
            "follow_status": null,
            "score": 15,
            "live_id":"454654646"
          },
          {
            "pfid": 1000044,
            "nickname": "嘻嘻哈哈😄💔1213",
            "headimg": "http://blob.ufile.ucloud.com.cn/45bd20f480c941f800a101bb484cb8be.jpg",
            "count": 0,
            "follow_status": null,
            "score": 13,
            "live_id":"454654646"
          },
          {
            "pfid": 1000048,
            "nickname": "珍珠奶茶😱芋頭紅豆薏米",
            "headimg": "http://blob.ufile.ucloud.com.cn/c8d240b8dfb870238431f43a392b312fba00a22c15230aa66f8e33cacc6debda",
            "count": 0,
            "follow_status": null,
            "score": 10,
            "live_id":"454654646"
          },
          {
            "pfid": 1000830,
            "nickname": "58665",
            "headimg": "http://blob.ufile.ucloud.com.cn/a765a85a32fde0343476d3d5a54ce686d9370cd4e35f4ac941661c2ab6746bcd",
            "count": 0,
            "follow_status": null,
            "score": 9,
            "live_id":"454654646"
          },
          {
            "pfid": 1000941,
            "nickname": "随意",
            "headimg": "http://playback-langlive.ufile.ucloud.cn/41395b6c4851bbc8d2ac49b643ea1420",
            "count": 0,
            "follow_status": 0,
            "score": 8,
            "live_id":"454654646"
          },
          {
            "pfid": 1000052,
            "nickname": "upper",
            "headimg": "http://blob.ufile.ucloud.com.cn/9256deb2c705849daef63ace024d4fe121e68f712eefdc585692247a52cf1920",
            "count": 0,
            "follow_status": null,
            "score": 7,
            "live_id":"454654646"
          },
          {
            "pfid": 1000733,
            "nickname": "查房",
            "headimg": "http://playback-langlive.ufile.ucloud.cn/8422f67af167df7d2b2caa78ac0a4773c4eade4575b343b5a82b7a48c4bf1427",
            "count": 0,
            "follow_status": null,
            "score": 6,
            "live_id":"454654646"
          },
          {
            "pfid": 1000012,
            "nickname": "c224002",
            "headimg": "http://blob.ufile.ucloud.com.cn/0d94ae9dc33721c87af78a7b2099f5a7ab7395972444c7ab979e3dea46e8ba2e",
            "count": 0,
            "follow_status": null,
            "score": 5,
            "live_id":"454654646"
          },
          {
            "pfid": 1000039,
            "nickname": "哇呀唔唔無武呃呃😂哇咔",
            "headimg": "http://blob.ufile.ucloud.com.cn/25f0e2243ac9f515ed3be2fb1854c8b7db8e04b195a088962179cf5937ae25dd",
            "count": 0,
            "follow_status": null,
            "score": 4,
            "live_id":null
          },
          {
            "pfid": 1000049,
            "nickname": "思考解决",
            "headimg": "http://blob.ufile.ucloud.com.cn/6b7f50b02ed10b72159ccf1f00a73ab9.jpg",
            "count": 0,
            "follow_status": null,
            "score": 3,
            "live_id":null
          },
          {
            "pfid": 1000040,
            "nickname": "🐾\"sting\"💤",
            "headimg": "http://blob.ufile.ucloud.com.cn/3f8f9f008d9d34a9b4cdffd32a06a3d8.jpg",
            "count": 0,
            "follow_status": null,
            "score": 2,
            "live_id":null
          },
          {
            "pfid": 1000010,
            "nickname": "gggggg",
            "headimg": "http://blob.ufile.ucloud.com.cn/f9cb7d8ac961fb9b3d5a882aea55bfe5",
            "count": 0,
            "follow_status": null,
            "score": 1,
            "live_id":null
          }
        ],
        "my": {
          "live_id": null,
          "live_key": "",
          "stream_direction": 0,
          "live_url": "",
          "headimg": "http://blob.ufile.ucloud.com.cn/c8d240b8dfb870238431f43a392b312fba00a22c15230aa66f8e33cacc6debda",
          "nickname": "珍珠奶茶😱芋頭紅豆薏米",
          "pfid": 1000048,
          "follow_status": 0
        }
      }
    }
var users=data.data.users;
sortData(users);
// 1 把人員按照積分从高到底排列,冒泡排序
function sortData(lists){
    for(var n=1;n<lists.length;n++){
        for(var k=0;k<lists.length-1;k++){
            var max=lists[k].score;
            var nextCount=lists[k+1].score;
            if(nextCount>max){
                var preData=lists[k];
                lists[k]=lists[k+1];
                lists[k+1]=preData;
            }
        }
    }
    console.log(lists);//此时lists就是按照积分高低排序之后的列表
}

 

posted @ 2017-05-10 18:24  逗伴不是瓣  阅读(1530)  评论(0编辑  收藏  举报