基于id对数组中的数组进行排序

时间:2017-08-09 18:24:17

标签: arrays swift sorting

我将此JSON代码存储在Core数据中:

{  
   "success":1,
   "data":[  
      {  
         "activity_id":2,
         "datetime":1501932600,
         "athlete":{  
            "athlete_id":1,
            "first_name":"Bram",
            "last_name":"N"
         },
         "comments":[  
            {  
               "comment_id":1,
               "athlete":{  
                  "athlete_id":1,
                  "first_name":"Bram",
                  "last_name":"N"
               },
               "timestamp":1501876800,
               "message":"Good job!"
            },
            {  
               "comment_id":3,
               "athlete":{  
                  "athlete_id":1,
                  "first_name":"Bram",
                  "last_name":"N"
               },
               "timestamp":1501876800,
               "message":"Awesome"
            }
         ],
         "goals":[  
            {  
               "goal_id":1,
               "sport_name":"Running",
               "step":1,
               "goal_name":"Duration",
               "amount":30,
               "data_displayed":{  
                  "data_displayed_id":1,
                  "top_left":"Duration",
                  "top_right":"Distance",
                  "bot_left":"Heartrate",
                  "bot_right":"Pace"
               },
               "dataset":{  
                  "dataset_id":1,
                  "duration":63,
                  "distance":11,
                  "calories":309,
                  "steps":5048,
                  "pace_min":4,
                  "pace_max":5,
                  "pace_avg":5,
                  "heartrate_min":90,
                  "heartrate_max":190,
                  "heartrate_avg":143,
                  "elevation":0,
                  "decrease":0
               },
               "locations":[  
                  {  
                     "location_id":1,
                     "latitude":50,
                     "longitude":50,
                     "timestamp":1501853400
                  },
                  {  
                     "location_id":2,
                     "latitude":50,
                     "longitude":51,
                     "timestamp":1501853700
                  }
               ],
               "splits_distance":[  
                  {  
                     "splits_distance_id":1,
                     "distance":1,
                     "duration":4
                  },
                  {  
                     "splits_distance_id":2,
                     "distance":2,
                     "duration":5
                  }
               ],
               "splits_duration":[  
                  {  
                     "splits_duration_id":1,
                     "duration":5,
                     "distance":0.87
                  },
                  {  
                     "splits_duration_id":2,
                     "duration":10,
                     "distance":1.75
                  }
               ],
               "graph_values":[  
                  {  
                     "graph_value_id":1,
                     "timestamp":1501853400,
                     "distance":0,
                     "calories":0,
                     "steps":0,
                     "heartrate":0,
                     "pace":0,
                     "elevation":0
                  },
                  {  
                     "graph_value_id":2,
                     "timestamp":1501853700,
                     "distance":1.1,
                     "calories":50,
                     "steps":105,
                     "heartrate":132,
                     "pace":4.3,
                     "elevation":10
                  },
                  {  
                     "graph_value_id":7,
                     "timestamp":1501854000,
                     "distance":1.8,
                     "calories":142,
                     "steps":264,
                     "heartrate":154,
                     "pace":4.8,
                     "elevation":8
                  },
                  {  
                     "graph_value_id":8,
                     "timestamp":1501854300,
                     "distance":2.6,
                     "calories":209,
                     "steps":402,
                     "heartrate":143,
                     "pace":5.3,
                     "elevation":15
                  }
               ]
            }
         ],
         "images":[  
            {  
               "image_id":1,
               "image_url":"www.trimeter.be\/image1"
            }
         ],
         "likes":[  
            {  
               "like_id":1,
               "athlete":{  
                  "athlete_id":1,
                  "first_name":"Bram",
                  "last_name":"N"
               }
            }
         ]
      },
      {  
         "activity_id":3,
         "datetime":1502137043,
         "athlete":{  
            "athlete_id":1,
            "first_name":"Bram",
            "last_name":"N"
         },
         "comments":[  
            {  
               "comment_id":4,
               "athlete":{  
                  "athlete_id":1,
                  "first_name":"Bram",
                  "last_name":"N"
               },
               "timestamp":1502149774,
               "message":"Test message, please ignore"
            }
         ],
         "goals":[  
            {  
               "goal_id":2,
               "sport_name":"Swimming",
               "step":1,
               "goal_name":"Distance",
               "amount":1500,
               "data_displayed":{  
                  "data_displayed_id":3,
                  "top_left":"Dit",
                  "top_right":"Is",
                  "bot_left":"Iets",
                  "bot_right":"Anders"
               },
               "dataset":{  
                  "dataset_id":2,
                  "duration":2,
                  "distance":2,
                  "calories":2,
                  "steps":2,
                  "pace_min":2,
                  "pace_max":2,
                  "pace_avg":2,
                  "heartrate_min":2,
                  "heartrate_max":2,
                  "heartrate_avg":2,
                  "elevation":2,
                  "decrease":2
               },
               "locations":[  

               ],
               "splits_distance":[  

               ],
               "splits_duration":[  

               ],
               "graph_values":[  

               ]
            },
            {  
               "goal_id":3,
               "sport_name":"Cycling",
               "step":2,
               "goal_name":"Distance",
               "amount":40000,
               "data_displayed":{  
                  "data_displayed_id":2,
                  "top_left":"1",
                  "top_right":"2",
                  "bot_left":"3",
                  "bot_right":"4"
               },
               "dataset":{  
                  "dataset_id":3,
                  "duration":3,
                  "distance":3,
                  "calories":3,
                  "steps":3,
                  "pace_min":3,
                  "pace_max":3,
                  "pace_avg":3,
                  "heartrate_min":3,
                  "heartrate_max":3,
                  "heartrate_avg":3,
                  "elevation":3,
                  "decrease":3
               },
               "locations":[  

               ],
               "splits_distance":[  

               ],
               "splits_duration":[  

               ],
               "graph_values":[  

               ]
            },
            {  
               "goal_id":4,
               "sport_name":"Running",
               "step":3,
               "goal_name":"Distance",
               "amount":10000,
               "data_displayed":{  
                  "data_displayed_id":4,
                  "top_left":"1234",
                  "top_right":"5678",
                  "bot_left":"4321",
                  "bot_right":"8765"
               },
               "dataset":{  
                  "dataset_id":4,
                  "duration":4,
                  "distance":4,
                  "calories":4,
                  "steps":4,
                  "pace_min":4,
                  "pace_max":4,
                  "pace_avg":4,
                  "heartrate_min":4,
                  "heartrate_max":4,
                  "heartrate_avg":4,
                  "elevation":4,
                  "decrease":4
               },
               "locations":[  

               ],
               "splits_distance":[  

               ],
               "splits_duration":[  

               ],
               "graph_values":[  

               ]
            }
         ],
         "images":[  
            {  
               "image_id":2,
               "image_url":"www.trimeter.be\/image2"
            }
         ],
         "likes":[  

         ]
      }
   ]
}

将数据存储在Core Data中后,我获取所有项目并将它们放入一个数组中以便以后使用。 由于这些项目没有按顺序保存,因此我使用它在数组的didSet中按时间戳排序:

allActivitiesArray = allActivitiesArray.sorted(by: { $0.timestamp > $1.timestamp })

现在我还需要对目标和评论进行排序,我该如何解决?我可以用上面的行以某种方式做到这一点吗?

我正在考虑做这样的事情:

for activity in allActivitiesArray {
    var allGoals = activity.goals?.allObjects
    allGoals = allGoals?.sorted(by: { ($0 as! Goal).id < ($1 as! Goal).id } )
}

然后将已排序的目标分配回活动,但它不会让我,因为我将[Any]分配给NSSet,所以我在这里丢失了一些。

非常感谢任何帮助/解释,谢谢!

0 个答案:

没有答案