且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在iPhone中按日期格式排序

更新时间:2023-01-06 23:17:39

   - (void) (NSXMLParser *)解析器foundCharacters:(NSString *)string 
{
[dateString appendString:string]; (NSString *)nameName namespaceURI(NSString *)namespaceURI qualifiedName :( NSString *)qName {
} $($)
$ NSDateFormatter * df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@dd.MM.yyyy]; //如果以不同的方式使用格式,请更改格式
NSDate * myDate = [df dateFromString:dateString];
[dateArray addObject:myDate];之后,您可以使用您所述的方式对数组进行排序。

I am new to iphone development.I am sorting a mutable array with respect to date.But it is sorting Using the date parameter consider it as string.I want to sort it by date.How can i achieve that .Please help me out.

 NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"pubDate" ascending:YES];
[stories sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];

Can i use selectors? if so how should i use it?.Thanks.

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{ 
    [dateString appendString:string];
}    

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{  
  NSDateFormatter *df = [[[NSDateFormatter alloc] init]autorelease];
  [df setDateFormat:@"dd.MM.yyyy"]; //change the format if you are using it in different way
  NSDate *myDate = [df dateFromString: dateString];
  [dateArray addObject:myDate];
}

after that you can sort the array just using the way you wrote in question.