0

我正在解析一个 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];
4

1 回答 1

-1

在 viewDidLoad() 中添加以下内容,用于按升序对 Array 数据进行排序。

        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);
于 2013-09-12T05:15:23.827 回答