如何对此数组进行排序包含日期和时间,我需要对相同日期进行排序,如果我有相同日期的多个条目,我需要根据时间按递减顺序对其进行排序,它将按照修整顺序显示所有条目。
data= {
{
dateAndTime = "04/03/2017 09:52 PM";
deviceImage = "";
dialOrReceive = 0;
number = "+918839752179";
profilePhoto = "";
},
{
dateAndTime = "02/03/2017 06:54 PM";
deviceImage = "";
deviceImage = "";
dialOrReceive = 0;
number = "+918839752179";
profilePhoto = "";
},
{
dateAndTime = "04/03/2017 05:36 PM";
deviceImage = "";
dialOrReceive = 0;
number = "+918839752179";
profilePhoto = "";
},
{
dateAndTime = "03/03/2017 04:48 PM";
deviceImage = "";
dialOrReceive = 0;
number = "+918839752179";
profilePhoto = "";
}, {
dateAndTime = "03/03/2017 06:48 PM";
deviceImage = "";
dialOrReceive = 0;
number = "+918839752179";
profilePhoto = "";
},{
dateAndTime = "02/03/2017 09:36 PM";
deviceImage = "";
dialOrReceive = 0;
number = "+918839752179";
profilePhoto = "";
},
}
答案 0 :(得分:1)
试试这个
- (void)sortDict {
NSDictionary *dict = @{
@"data" : @[@{
@"dateAndTime" : @"04/03/2017 09:52 PM",
@"deviceImage" : @"",
@"dialOrReceive" : @0,
@"number" : @"+918839752179",
@"profilePhoto" : @""
},
@{
@"dateAndTime" : @"02/03/2017 06:54 PM",
@"deviceImage" : @"",
@"dialOrReceive" : @0,
@"number" : @"+918839752179",
@"profilePhoto" : @""
},
@{
@"dateAndTime" : @"04/03/2017 05:36 PM",
@"deviceImage" : @"",
@"dialOrReceive" : @0,
@"number" : @"+918839752179",
@"profilePhoto" : @""
},
@{
@"dateAndTime" : @"03/03/2017 04:48 PM",
@"deviceImage" : @"",
@"dialOrReceive" : @0,
@"number" : @"+918839752179",
@"profilePhoto" : @""
},
@{
@"dateAndTime" : @"03/03/2017 06:48 PM",
@"deviceImage" : @"",
@"dialOrReceive" : @0,
@"number" : @"+918839752179",
@"profilePhoto" : @""
},
@{
@"dateAndTime" : @"02/03/2017 09:36 PM",
@"deviceImage" : @"",
@"dialOrReceive" : @0,
@"number" : @"+918839752179",
@"profilePhoto" : @""
},
@{
@"dateAndTime" : @"02/03/2017 07:36 AM",
@"deviceImage" : @"",
@"dialOrReceive" : @0,
@"number" : @"+918839752179",
@"profilePhoto" : @""
}
]
};
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = @"dd/MM/yyyy hh:mm a";
NSArray *arr = [dict objectForKey:@"data"];
arr = [arr sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
NSDate *date1 = [formatter dateFromString:[obj1 objectForKey:@"dateAndTime"]];
NSDate *date2 = [formatter dateFromString:[obj2 objectForKey:@"dateAndTime"]];
return [date2 compare:date1];
}];
NSLog(@"%@", arr);
}