简体   繁体   中英

iOS Parse RSS Into TableView By Alphabetical Order

I am parsing an XML file that just contains title, description, and link for each item in the XML. I have done sorting before using the pubDate section, but on this, I would just like it to be either in alphabetical order, or in the order listed in the XML. Suggestions on doing this? On past versions I have sorted it like this:

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];

Add the following in viewDidLoad(),Which is for sorting Array data by ascending order..

        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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM