I have this XML which I display in UITableView:
<result>
<trip duration="03:30">
<takeoff date="2010-06-19" time="18:40" city="Moscow"/>
<landing date="2010-06-19" time="20:10" city="Novgorod"/>
<flight carrier="Rossiya" number="8395" eq="320"/>
<price>13429.00</price>
</trip>
<trip duration="03:40">
<takeoff date="2010-06-19" time="09:20" city="Omsk"/>
<landing date="2010-06-19" time="11:15" city="Paris"/>
<flight carrier="AirFrance" number="1145" eq="320"/>
<price>13229.00</price>
</trip>
<trip duration="03:50">
<takeoff date="2010-06-19" time="07:20" city="Omsk"/>
<landing date="2010-06-19" time="14:15" city="Barcelona"/>
<flight carrier="AirFrance" number="1100" eq="320"/>
<price>13329.00</price>
</trip>
</result>
I’m using this code to save it:
if ([elementname isEqualToString:@"trip"]) {
currentTweet = [Tweet alloc];
isStatus = YES;
flightTime = [attributeDict objectForKey:@"duration"];
currentTweet.flightDuration = [NSString stringWithFormat:@"Flight duration is: %@", flightTime];
}
if([elementname isEqualToString:@"takeoff"]) {
takeoffPlace = [attributeDict objectForKey:@"city"];
}
if ([elementname isEqualToString:@"landing"]) {
landingPlace = [attributeDict objectForKey:@"city"];
currentTweet.content = [NSString stringWithFormat:@"%@ - %@", takeoffPlace, landingPlace];
}
if ([elementname isEqualToString:@"takeoff"]) {
takeoffDate = [attributeDict objectForKey:@"date"];
takeoffTime = [attributeDict objectForKey:@"time"];
currentTweet.takeOffAll = [NSString stringWithFormat:@"Take off date %@. Take off time %@", takeoffDate, takeoffTime];
}
if ([elementname isEqualToString:@"landing"]) {
landingDate = [attributeDict objectForKey:@"date"];
landingTime = [attributeDict objectForKey:@"time"];
currentTweet.landingAll = [NSString stringWithFormat:@"Landing date %@. Landing time %@", landingDate, landingTime];
}
if ([elementname isEqualToString:@"flight"]) {
companyName = [attributeDict objectForKey:@"carrier"];
companyNumber = [attributeDict objectForKey:@"number"];
eqStuff = [attributeDict objectForKey:@"eq"];
currentTweet.companyInfo = [NSString stringWithFormat:@"Carrier: %@ Flight number: %@ Eq: %@", companyName, companyNumber, eqStuff];
}
I need to sort my TableView by 2 parameters:
-
The
<trip duration="03:50">time -
The
<price>
I’m not sure whether I’m using the right structure – basically all the data is NSStrings assigned to a separate data class which are then assigned to UILabels inside UITableView cell.
What would be the best way to sort the data by time and price? Any ideas on how I should reformat the code?
P.S. I’m using SegmentedControl to reload the TableView and sort it accordingly.
You can store your results in an array of dictionary entries, which you can then sort the array using
sortedArrayUsingComparatorlike I demonstrate at the end of this answer.For example, my parser was as follows:
You’ll notice that that
parserDidEndDocumentcallssortResultsByDurationAndPricewhich looks like:Clearly, you can just tweak your
sortResultsbased upon your personal array of dictionary entries, but this should give you an idea of how it could work.