我是 iOS 的新手,我正在将我的应用与服务集成
并形成我正在获得的服务"日期"值和我将日期值存储在**NSMutableArray**
中,那么我如何按升序排列我的数组列表请看下面
如何更改以下数组列表日期格式,如" 2016年2月17日20:25"请帮帮我一个
(
"02/17/2016 1415",
"02/13/2016 1415",
"02/14/2016 1415",
"02/18/2016 1534",
"02/21/2016 1534",
"02/22/2016 1534",
"02/18/2016 1534",
"02/20/2016 1534",
)
答案 0 :(得分:1)
以递增顺序排序NsmutableArray的代码:
NSMutableArray *arr = @[@"02/17/2016",
@"02/13/2016",
@"02/14/2016",
@"02/18/2016",
@"02/21/2016",
@"02/22/2016",
@"02/18/2016",
@"02/20/2016"].mutableCopy;
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: YES];
NSArray *arr1 = [arr sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];
和输出:
(
"02/13/2016",
"02/14/2016",
"02/17/2016",
"02/18/2016",
"02/18/2016",
"02/20/2016",
"02/21/2016",
"02/22/2016"
)
更改日期的代码Formate:
NSString *dateString = @"11-13-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM-dd-yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
// Convert date object into desired format
[dateFormatter setDateFormat:@"dd MMM yyyy"];
NSString *newDateString = [dateFormatter stringFromDate:date];
答案 1 :(得分:1)
<强>排序强>
NSSortDescriptor* OrderBy = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: YES]; // for ascending use `YES`, for Descending USE `NO`
NSArray *finalArar = [yourmutableArrayName sortedArrayUsingDescriptors: [NSArray arrayWithObject: OrderBy]];
更改dateFormat
for (NSString *dte in finalArar)
{
NSDateFormatter *format = [[NSDateFormatter alloc] init];
//your Date String is 02/17/2016
[format setDateFormat:@"MM/dd/yyyy"];
// for example like this
[format setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// or use this [format setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSDate *parsed = [format dateFromString:dte];
//final String is 02 Feb 2016
[format setDateFormat:@"dd MMM yyyy"];
NSString *newDateString = [format stringFromDate:parsed];
NSLog(@"final date==%@",newDateString)
}