JS对象数组根据对象的某个属性进行排序

 1 /**
 2    * 已选择的排前面
 3    */
 4   sortByCheckStatus: function(branch1, branch2) {
 5     let b1 = branch1.is_check ? 1 : 0;
 6     let b2 = branch2.is_check ? 1 : 0;
 7     return b2 - b1;
 8   },
 9 
10   /**
11    * 查询门店列表
12    */
13   queryBranchList: function() {
14     let branchList = this.data.branch_list;
15     let kw = this.data.keyword;
16     let showBranchList = [];
17     for (let i = 0; i < branchList.length; i++) {
18       if (branchList[i].branch_no.indexOf(kw) > -1 || branchList[i].branch_name.indexOf(kw) > -1) {
19         showBranchList.push(branchList[i]);
20       }
21     }
22     showBranchList = showBranchList.sort(this.sortByCheckStatus);
23     this.setData({
24       show_branch_list: showBranchList
25     });
26   },
 1   /**
 2    * 获取门店列表
 3    */
 4   getBranchList: function() {
 5     let _this = this;
 6     let postData = {
 7       'shopid': app.globalData.shopid,
 8     };
 9     wx.showLoading({
10       title: '门店获取中...',
11       mask: true
12     })
13     _this.setData({
14       branch_list: [],
15 
16     });
17     http.httpGet(api.GetBranchList, postData, (res) => {
18       console.log(res);
19       wx.hideLoading();
20       _this.setData({
21         load_complete: true
22       });
23       if (!res) {
24         return;
25       }
26 
27       if (res.success) {
28         let _result = res.result;
29         if (_result.success) {
30           _this.setData({
31             branch_list: _result.data,
32             show_branch_list: _result.data
33           });
34           this.initAdminBranch();
35 
36         } else {
37           utils.showToastMessage(_result);
38         }
39       } else {
40         utils.showToastMessage(res);
41       }
42 
43     });
44   },
45 
46   initAdminBranch: function() {
47     if (this.data.oper_type == 'choose_branch') {
48       let branch_no_list = this.data.branch_no_list;
49       let branchList = this.data.show_branch_list;
50       if (branch_no_list) {
51         let branchNoArray = branch_no_list.split(',');
52         for (let i = 0; i < branchNoArray.length; i++) {
53           let branchNo = branchNoArray[i];
54           if (branchNo) {
55             for (let j = 0; j < branchList.length; j++) {
56               let branch = branchList[j];
57               if (branchNo == branch.branch_no) {
58                 branch.is_check = true;
59               }
60             }
61           }
62         }
63 
64         branchList = branchList.sort(this.sortByCheckStatus);
65         this.setData({
66           show_branch_list: branchList
67         });
68       }
69     }
70   },

 

 

posted @ 2020-04-07 16:55  w1433189426  阅读(1298)  评论(0编辑  收藏  举报