views:

65

answers:

2

With this

    NSSortDescriptor *lessonDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lesson" ascending:YES];
[resultArray sortUsingDescriptors:[NSArray arrayWithObject:lessonDescriptor]];

it's sorting the "lesson"-objects to

(
    {
    lesson = 9;
    subject = bg;
},
    {
    lesson = 8;
    subject = bg;
},
    {
    lesson = 11;
    subject = CAE;
},
    {
    lesson = 11;
    subject = CAE;
},
    {
    lesson = 10;
    subject = CAE;
},
    {
    lesson = 10;
    subject = CAE;
},
    {
    lesson = 5;
    subject = Gg;
},
    {
    lesson = 4;
    subject = G;
},
    {
    lesson = 3;
    subject = G;
},
    {
    lesson = 2;
    subject = M;
},
    {
    lesson = 1;
    subject = M;
}

)

as you can see it's not counting correctly.. Can somebody help?

thanks alot

edit:

don't know how to implement the value thing =)

else if ([currentElement isEqualToString:@"lesson"]) 
{ 

    NSString *trimmedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSLog(@"trimmed: %@",trimmedString);
    int value = [trimmedString intValue];   ??
    [currentSummary appendString:value];  ??

    [currentSummary appendString:trimmedString]; 
} 
+2  A: 

You have to trim the string before converting the 'lesson' into numbers in order for the sort descriptors to work correctly. Otherwise, the sort descriptor sees only string values not numbers. This is how you remove the whitespace and newline characters:

NSString *stringValue = [parsedValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

A call to [stringValue intValue] will get the integer value of the string

int value = [stringValue intValue];

Save this value into the array before calling the sort descriptors on this attribute.

Tell me if it works.

sfa
Well I tried to trim it like this: NSString *trimmedString = [resultArray stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];But it says NSMutableArray doesnt respond to it...
dav3
hi dav3, you have to trim separately each string when you have the result, not by trimming the array, I suggest that you review some guides on the language Objective-C itself before diving too far into iPhone programming. This is very basic task that you will have to use quite often in iPhone programming. Cheers!
sfa
problem nr1. I store everything in Arrays or Dictionaries.problem nr2. since this a graduation work and more a self-experiment in making an app I have not the time to study the hole thing.. Of course I reviewed several guides, but parsing XML and trimming it afterwards never came across my way. =)
dav3
hi, check the edited answer if it helps. Cheers!
sfa
hi dav3, try to edit your question, it's impossible to read your code in the comment :-s
sfa
haha =) sorry sfa... hope u can see it now... thanks!
dav3
Well I think my problem is not solvable.. I parse an XML and take the important bits in Strings wich I store at the end in an Array to populate the cells.. Now I need to sort them and thats impossible with strings.. wow I'm smart.. =) ;)
dav3
what do I have to take to store Strings and Numbers (/values) at the same time..? Friend of mine told me that's going to be shitty..
dav3
no, you store the lesson as numbers in the array by converting the value from the parser. In the controller, you just need to sort by this lesson numbers, displaying the result by converting these numbers to string again. Sorry it's a little bit confused your code, I am not sure what you are trying to do with the above code. Hope it helps.
sfa
A: 

It sorted it just right. After all, every entry is a string, and in a string "10" is before "9" after all...

Now if you wanted to sort it the way you expected to sort it, you either need to hold NSNuumber objects and sort that, or have strings held in normalized form like "08", "09", "10", and then possibly trim off leading 0's from the string later.

Kendall Helmstetter Gelner