Sort NSMutableArray
Asked Answered
S

3

0

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.

Sharisharia answered 24/8, 2010 at 13:18 Comment(0)
C
1

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.

Calvin answered 24/8, 2010 at 19:52 Comment(10)
how to declare the property with a numeric type ?Sharisharia
Give it a numeric type when you declare it. The documentation I linked to shows you how to declare a property.Calvin
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 NSMutableArraySharisharia
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)?Calvin
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 ^__^Sharisharia
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.Calvin
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 > NSMutableArraySharisharia
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.Calvin
Yes I try to put it into array but I don't know how to get data from that array !?Sharisharia
The same way you get the dictionaries out. If you want a more specific answer, you should ask a more specific (and separate) question.Calvin
P
1

The best way to do this is to store your numbers as NSNumbers:

[index addObject:[[NSNumber alloc] initWithInt:number]];

Then you can sort your array using the [NSNumber compare:] method:

[index sortUsingSelector:@selector(compare:)];
Pinkney answered 5/5, 2011 at 5:17 Comment(0)
U
0
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?

Unvoice answered 24/8, 2010 at 13:26 Comment(2)
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.Calvin
this is my code: [item setObject:[NSNumber numberWithInt:[[[_modeObject valueForKey:@"point"] description] intValue]] forKey:@"point"]; maybe help you ^____^Sharisharia

© 2022 - 2024 — McMap. All rights reserved.