views:

110

answers:

3

please help me I want to sort NSMutableArray and yes I can do this by

NSSortDescriptor *sortPoint = [[NSSortDescriptor alloc] initWithKey:@"point" ascending:YES];
[hightScore sortUsingDescriptors:[NSArray arrayWithObject:sortPoint]];

but in NSMuatableArray(hightScore), I have 2 NSMutableDictionary like this

[item setObject:@"player_name" forKey:@"name"];
[item setObject:@"100" forKey:@"point"];

and when I sort by keyword "point" that is string it give me like this 100 > 20 > 30 > 50 instead should be 20 > 30 > 50 > 100

have any idea pleassss.

A: 
NSMutableArray* tempArray = [NSMutableArray arrayWithArray:anArray];
[tempArray sortUsingSelector:@selector(caseInsensitiveCompare:)];

I use this in some code? Will this work?

Also, just noticed you are setting the array objects as strings which I'm assuming will also be an issue. Try adding them as NSIntegers or ints... does that work at all?

Thomas Clayson
The problem is that the questioner is comparing strings in the default manner, which is not a numeric comparison. `caseInsensitiveCompare:` isn't a numeric comparison, either.
Peter Hosey
hmm, fair enough
Thomas Clayson
this is my code:[item setObject:[NSNumber numberWithInt:[[[_modeObject valueForKey:@"point"] description] intValue]] forKey:@"point"];maybe help you ^____^
mr.octobor
+1  A: 

There are three solutions:

  1. Put NSNumber instances into the dictionaries in place of the strings (e.g., @"100") you currently have. NSNumber objects should compare themselves numerically.
  2. Send the array a sortUsingComparator: or sortUsingFunction:context: message instead of sortUsingDescriptors:, passing a block or function that retrieves the strings and compares them numerically (by sending them compare:options: messages with the NSNumericSearch option).

    Note that the block or function will be passed the whole dictionaries, not the strings, since it has no way to know which strings to extract, so you'll have to get the strings out of the dictionaries yourself in order to compare them.

  3. Replace the dictionaries with model objects that have properties in place of the dictionary keys. If you declare the property with a numeric type (e.g., NSUInteger), then when the array uses KVC to access the property, it will get NSNumber objects, which, as noted under #1 above, should compare themselves numerically.

I would go with #3.

Peter Hosey
how to declare the property with a numeric type ?
mr.octobor
Give it a numeric type when you declare it. The documentation I linked to shows you how to declare a property.
Peter Hosey
very thank I use choice and it work!. this is my code for everyone[item setObject:[NSNumber numberWithInt:[[[_modeObject valueForKey:@"point"] description] intValue]] forKey:@"point"];**item is NSMutableArray
mr.octobor
I use choice #3
mr.octobor
mr.october: That's not #3, that's #1, and implemented in a very roundabout way. Isn't this code going to cause problems later on when other code expects to find strings there and gets numbers instead? Why not just create numbers and put them into the dictionary in the first place? And why not actually do #3 (replace your dictionaries with model objects)?
Peter Hosey
I must read data from core data and put them in to NSMutableArray.I'm newbie in Xcode and very thank for your help because I looking for this solution more then 3 days.very thanks ^__^
mr.octobor
mr.octobor: You don't “read data from Core Data”; Core Data gives you managed objects, which are the model objects you should be using. You should set the `point` attribute of your high-score entity to be of the Integer 32 type, not a string. Then, just make a mutable copy of the array of score objects, and sort that by the `point` key using NSSortDescriptor. You should not create dictionaries for this at all.
Peter Hosey
oh! really I don't know it before.first my code is[item setObject:[[_modeObject valueForKey:@"name"] description] forKey:@"name"];[item setObject:[[_modeObject valueForKey:@"point"] description] forKey:@"point"];[hightScore addObject:[item copy]];but after I have your suggestion I try and it OK.[item setObject:[[_modeObject valueForKey:@"name"] description] forKey:@"name"];[item setObject:[NSNumber numberWithInt:[[[_modeObject valueForKey:@"point"] description] intValue]] forKey:@"point"];[hightScore addObject:[item copy]];*item > NSMutableDictionary*hightScore > NSMutableArray
mr.octobor
No, that's not it. There is no need to create the `item` dictionary at all. (Also, you're leaking it when you put it into the array, as you're not releasing it.) Just put the model objects into the array and sort it.
Peter Hosey
Yes I try to put it into array but I don't know how to get data from that array !?
mr.octobor
The same way you get the dictionaries out. If you want a more specific answer, you should ask a more specific (and separate) question.
Peter Hosey
OK Thanks ^_____^
mr.octobor
A: 

how to declare the property with a numeric type ?

mr.octobor
See the documentation I linked to in my answer.
Peter Hosey