将项添加到NSMutableArray并按字母顺序排序

时间:2013-12-11 19:20:01

标签: ios xml parsing nsmutablearray

我通常通过解析XML解析博客应用程序,然后排序以获取pubDate并首先显示最新的。为此,我在解析完成时使用它:

NSMutableArray *entries = [NSMutableArray array];
            [self parseFeed:doc.rootElement entries:entries];

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                for (RSSEntryDirectory *entry in entries) {

                    int insertIdx = [_allEntries indexForInsertingObject:entry sortedUsingBlock:^(id a, id b) {
                        RSSEntryDirectory *entry1 = (RSSEntryDirectory *) a;
                        RSSEntryDirectory *entry2 = (RSSEntryDirectory *) b;
                        return [entry1.articleDate compare:entry2.articleDate];
                    }];

                    [_allEntries insertObject:entry atIndex:insertIdx];
                    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]]
                                          withRowAnimation:UITableViewRowAnimationRight];

                }

            }];

我想做的只是从title标签中取出文本,并按字母顺序添加到NSMutableArray。

1 个答案:

答案 0 :(得分:1)

[NSArray indexForInsertingObject:sortedUsingBlock:]是一种未记录的方法,我不建议使用它。

以下是如何做你想做的事:

  1. 将您的参赛作品放入_allEntries
  2. 对数组进行排序:
  3.     NSArray *sortedEntries = [_allEntries sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    
            RSSEntryDirectory *entry1 = (RSSEntryDirectory *) a;
            RSSEntryDirectory *entry2 = (RSSEntryDirectory *) b;
            return [entry1.articleDate compare:entry2.articleDate];
          }];
    
    1. 使用sortedEntries
    2. 填充表格视图