我正在解析一个XML文件,该文件只包含XML中每个项目的标题,描述和链接。我在使用pubDate部分之前已经完成了排序,但就此而言,我希望它可以按字母顺序排列,也可以按XML中列出的顺序排序。这样做的建议?在过去的版本中,我对它进行了如下排序:
for (RSSEntry *entry in entries) {
int insertIdx = [_allEntries indexForInsertingObject:entry sortedUsingBlock:^(id a, id b) {
RSSEntry *entry1 = (RSSEntry *) a;
RSSEntry *entry2 = (RSSEntry *) b;
return [entry1.articleDate compare:entry2.articleDate];
}];
[_allEntries insertObject:entry atIndex:insertIdx];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]]
withRowAnimation:UITableViewRowAnimationRight];
答案 0 :(得分:-1)
在viewDidLoad()中添加以下内容,用于按升序对数组数据进行排序。
artistArray=[[NSMutableArray alloc] init];
NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
[dic setObject:@"ravi" forKey:@"artist"];
[artistArray addObject:dic];
[dic release];
dic=[[NSMutableDictionary alloc] init];
[dic setObject:@"srinu" forKey:@"artist"];
[artistArray addObject:dic];
[dic release];
dic=[[NSMutableDictionary alloc] init];
[dic setObject:@"bobby" forKey:@"artist"];
[artistArray addObject:dic];
[dic release];
dic=[[NSMutableDictionary alloc] init];
[dic setObject:@"xyz" forKey:@"artist"];
[artistArray addObject:dic];
[dic release];
NSLog(@"before sorting--%@",artistArray);
NSSortDescriptor *sortByName=[NSSortDescriptor sortDescriptorWithKey:@"artist" ascending:YES];
NSArray *sortDescriptors=[NSArray arrayWithObject:sortByName];
NSArray *sortedArray=[artistArray sortedArrayUsingDescriptors:sortDescriptors];
NSLog(@"After sorting---%@",sortedArray);