views:

74

answers:

3

so I want to parse an xml and display (in a UITableView) the "subjects" in ascending order depending on "lessons"

XML:

<parfumeo_kkn>
<tt_timetable>
    <id>1015</id>
    <day>1</day>
    <class_id>98</class_id>
    <lesson>9</lesson>
    <teacher>bt</teacher>
    <room>V1.2</room>
    <subject>M</subject>
    <pr_id>54</pr_id>
</tt_timetable>
<tt_timetable>
    <id>1014</id>
    <day>1</day>
    <class_id>98</class_id>
    <lesson>8</lesson>
    <teacher>bt</teacher>
    <room>V1.2</room>
    <subject>bg</subject>
    <pr_id>54</pr_id>
</tt_timetable>

ect.

(In this little example there would be "bg" before "M".) I allready set up my parser but it's not working in the order I want it to. thx for help

+1  A: 

You should separate the logic of the parser and how you display the results in the table views. The parser just does its task of parsing the xml, the controller for the table view will do a little bit extra task by ordering the results from the parser by using NSSortDescriptor, an example follows:

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lesson" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

Yes, previously I am assuming you're using Core Data, in case you just have a normal array, then you can just simply call:

    NSArray *myArray = [xmlResultArray sortedArrayUsingDescriptors:sortDescriptors];

Now you can use myArray to populate to your table view controller.

Hope this helps!

sfa
thank you! but how to correctly set up this "fetchRequest"?
dav3
He is assuming you are using core data.
JeremyP
thanks alot.. but still not working.. I put your lines in -(void)parserDidEnddocument: NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"lesson" ascending:YES]autorelease]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [stories sortedArrayUsingDescriptors:sortDescriptors];maybe I got it wrong but my output remains the same..
dav3
hi dav3, see my edited answer. You have to use the result from the call to sortedArrayUsingDescriptors to populate to the table view. I thought you know that, sorry.
sfa
thanks sfa... I just redifined my array instead of using a new one... resultArray = [resultArray sortedArrayUsingDescriptors:sortDescriptors];don't know if that's clever but it kind of worked..
dav3
It works.. thanks!
dav3
You're welcome! Consider accepting the answer as it will let other people know where the answer to your question is!
sfa
A: 

Maybe, you can parse you XML file, put objects in a array then sort this array ?

NSArray *myArray = [parser parseTheFile:file];
[self sortTheArray:myArray];
dwursteisen
the response just above is better than mine.
dwursteisen
=) but I wanted to thank you too... unfortunately I'm too nooby to use it correctly.. :-/
dav3
+1 for the honest self-assessment
No one in particular
A: 

To sort an array you just do this.

NSArray *sortedPairs = [originalArray sortedArrayUsingSelector:@selector(compare:)];

If you have a dictionary and want to get a sorted array by keys you'd

NSArray *arr = [dictionary keysSortedByValueUsingSelector:@selector(compare:)];

once you have your array you can enumerate as you wish.

Amanuel