sortedArrayUsingComparator
結果を辞書エントリの配列に保存し、この回答の最後に示すように配列を並べ替えることができます。
たとえば、私のパーサーは次のとおりです。
@interface XmlParserViewController () <NSXMLParserDelegate>
{
NSMutableArray *trips;
NSMutableDictionary *currentTrip;
NSMutableString *currentElement;
}
@end
@implementation XmlParserViewController
- (void)viewDidLoad
{
[super viewDidLoad];
trips = [[NSMutableArray alloc] init];
// clearly I'm reading your XML from a file in my bundle, but you'll get it
// how ever you're retrieving it from your remote source
NSString *filename = [[NSBundle mainBundle] pathForResource:@"results" ofType:@"xml"];
NSData *data = [NSData dataWithContentsOfFile:filename];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
}
#pragma mark - NSXMLParserDelegate methods
#define kRowElementTag @"trip"
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
NSArray *attributeElementNames = @[@"takeoff", @"landing", @"flight"];
NSArray *foundCharacterElementNames = @[@"price"];
if ([elementName isEqualToString:kRowElementTag])
{
currentTrip = [[NSMutableDictionary alloc] init];
[trips addObject:currentTrip];
if (attributeDict)
[currentTrip setObject:attributeDict forKey:elementName];
}
else if (currentTrip)
{
if ([attributeElementNames containsObject:elementName])
{
if (attributeDict)
[currentTrip setObject:attributeDict forKey:elementName];
}
else if ([foundCharacterElementNames containsObject:elementName] && currentElement == nil)
{
// you can change this to just grab a few fields ... add whatever fields you want to this
currentElement = [[NSMutableString alloc] init];
[currentTrip setObject:currentElement forKey:elementName];
}
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:kRowElementTag])
{
currentTrip = nil;
}
else if (currentElement)
{
currentElement = nil;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if (currentElement)
{
[currentElement appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSLog(@"%s error=%@", __FUNCTION__, parseError);
// we should handle the error here
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"Order in XML:");
for (NSDictionary *trip in trips)
{
NSLog(@" %@ %@ %@ %@ %@ %@", trip[@"flight"][@"carrier"], trip[@"flight"][@"eq"], trip[@"takeoff"][@"date"], trip[@"takeoff"][@"time"], trip[@"trip"][@"duration"], trip[@"price"]);
}
NSArray *sortedTrips = [self sortResultsByDurationAndPrice];
NSLog(@"Sorted order:");
for (NSDictionary *trip in sortedTrips)
{
NSLog(@" %@ %@ %@ %@ %@ %@", trip[@"flight"][@"carrier"], trip[@"flight"][@"eq"], trip[@"takeoff"][@"date"], trip[@"takeoff"][@"time"], trip[@"trip"][@"duration"], trip[@"price"]);
}
// you can now update your user interface or do whatever at this point
//
// [self.tableView reloadData];
}
次のようにparserDidEndDocument
呼び出していることに気付くでしょう。sortResultsByDurationAndPrice
- (NSArray *)sortResultsByDurationAndPrice
{
NSArray *sortedTrips = [trips sortedArrayUsingComparator: ^(id trip1, id trip2) {
double duration1 = [self durationFromString:trip1[@"trip"][@"duration"]];
double duration2 = [self durationFromString:trip2[@"trip"][@"duration"]];
double price1 = [trip1[@"price"] doubleValue];
double price2 = [trip2[@"price"] doubleValue];
if (duration1 < duration2 || (duration1 == duration2 && price1 < price2)) {
return (NSComparisonResult)NSOrderedAscending;
}
if (duration1 > duration2 || (duration1 == duration2 && price1 > price2)) {
return (NSComparisonResult)NSOrderedDescending;
}
return (NSComparisonResult)NSOrderedSame;
}];
return sortedTrips;
}
// this assumes that duration is in the form of "h:mm"
- (double)durationFromString:(NSString *)durationString
{
NSArray *durationArray = [durationString componentsSeparatedByString:@":"];
return [durationArray[0] doubleValue] + [durationArray[1] doubleValue] / 60.0;
}
// sort by date and time not used, but was part of my original answer
// so I provide it here for the sake of completeness
- (NSArray *)sortResultsByDateTime
{
NSArray *sortedTrips = [trips sortedArrayUsingComparator: ^(id trip1, id trip2) {
NSComparisonResult dateComparison = [trip1[@"takeoff"][@"date"] compare:trip2[@"takeoff"][@"date"]];
NSComparisonResult timeComparison = [trip1[@"takeoff"][@"time"] compare:trip2[@"takeoff"][@"time"]];
if (dateComparison == NSOrderedAscending || (dateComparison == NSOrderedSame && timeComparison == NSOrderedAscending)) {
return (NSComparisonResult)NSOrderedAscending;
}
if (dateComparison == NSOrderedDescending || (dateComparison == NSOrderedSame && timeComparison == NSOrderedDescending)) {
return (NSComparisonResult)NSOrderedDescending;
}
return (NSComparisonResult)NSOrderedSame;
}];
return sortedTrips;
}
明らかに、sortResults
辞書エントリの個人的な配列に基づいて微調整することができますが、これにより、それがどのように機能するかについてのアイデアが得られるはずです.