如何对 ios 中的 NSMutableArray 中的时间进行排序
how to sort the time in a NSMutableArray in ios
我在 NSMutableArray 中有一组字典。现在我想按时间升序和降序排序这是我的数组值。
(
{
StartTime = "09:00 AM";
TravelsName = "ABC Travels";
},
{
StartTime = "07:30 AM";
TravelsName = "XYZ Travels ";
},
{
StartTime = "06:45 PM";
TravelsName = "GSP Travels";
},
{
StartTime = "05:00 PM";
TravelsName = "Madura Travels";
},
{
StartTime = "12:45 AM";
TravelsName = "MJT Travels";
},
{
StartTime = "12:45 PM";
TravelsName = "OPR Travels";
},
{
StartTime = "01:00 AM";
TravelsName = "VMS Travels";
}
)
请帮助我,我已经对 NSMutableArray 中的 NSDate 集进行了排序
通过以下代码
//To sort the Time Array
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mm a"];
NSMutableArray *dates = [NSMutableArray arrayWithCapacity:times.count];
for (NSString *timeString in times)
{
NSDate *date = [dateFormatter dateFromString:timeString];
[dates addObject:date];
}
[dates sortUsingSelector:@selector(compare:)];
NSMutableArray *sortedTimes = [NSMutableArray arrayWithCapacity:dates.count];
for (NSDate *date in dates)
{
NSString *timeString = [dateFormatter stringFromDate:date];
[sortedTimes addObject:timeString];
}
但我不知道如何对 NSMutableArray 中的字典进行排序帮助我。
最好使用NSArray
中已经提供的排序方法。应该这样做:
NSArray *stortedArray = [unsortedArray sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *_Nonnull obj1, NSDictionary *_Nonnull obj2) {
return [obj1[@"StartTime"] compare:obj2[@"StartTime"]];
}];
或者如果 StartTime
仍然是一个字符串:
NSArray *stortedArray = [unsortedArray sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *_Nonnull obj1, NSDictionary *_Nonnull obj2) {
return [[dateFormatter dateFromString:obj1[@"StartTime"]] compare:[dateFormatter dateFromString:obj2[@"StartTime"]]];
}];
要更改 ascending/descending 顺序,您只需交换块中使用的 obj1
和 obj2
:
return [obj2[@"StartTime"] compare:obj1[@"StartTime"]];
我在 NSMutableArray 中有一组字典。现在我想按时间升序和降序排序这是我的数组值。
(
{
StartTime = "09:00 AM";
TravelsName = "ABC Travels";
},
{
StartTime = "07:30 AM";
TravelsName = "XYZ Travels ";
},
{
StartTime = "06:45 PM";
TravelsName = "GSP Travels";
},
{
StartTime = "05:00 PM";
TravelsName = "Madura Travels";
},
{
StartTime = "12:45 AM";
TravelsName = "MJT Travels";
},
{
StartTime = "12:45 PM";
TravelsName = "OPR Travels";
},
{
StartTime = "01:00 AM";
TravelsName = "VMS Travels";
}
)
请帮助我,我已经对 NSMutableArray 中的 NSDate 集进行了排序 通过以下代码
//To sort the Time Array
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mm a"];
NSMutableArray *dates = [NSMutableArray arrayWithCapacity:times.count];
for (NSString *timeString in times)
{
NSDate *date = [dateFormatter dateFromString:timeString];
[dates addObject:date];
}
[dates sortUsingSelector:@selector(compare:)];
NSMutableArray *sortedTimes = [NSMutableArray arrayWithCapacity:dates.count];
for (NSDate *date in dates)
{
NSString *timeString = [dateFormatter stringFromDate:date];
[sortedTimes addObject:timeString];
}
但我不知道如何对 NSMutableArray 中的字典进行排序帮助我。
最好使用NSArray
中已经提供的排序方法。应该这样做:
NSArray *stortedArray = [unsortedArray sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *_Nonnull obj1, NSDictionary *_Nonnull obj2) {
return [obj1[@"StartTime"] compare:obj2[@"StartTime"]];
}];
或者如果 StartTime
仍然是一个字符串:
NSArray *stortedArray = [unsortedArray sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *_Nonnull obj1, NSDictionary *_Nonnull obj2) {
return [[dateFormatter dateFromString:obj1[@"StartTime"]] compare:[dateFormatter dateFromString:obj2[@"StartTime"]]];
}];
要更改 ascending/descending 顺序,您只需交换块中使用的 obj1
和 obj2
:
return [obj2[@"StartTime"] compare:obj1[@"StartTime"]];