views:

788

answers:

2

Hi !

I'm currently using NSMutableArrays in my developments to store some data taken from an HTTP Servlet.

Everything is fine since now I have to sort what is in my array.

This is what I do :

NSMutableArray *array = [[NSMutableArray arrayWithObjects:nil] retain];
[array addObject:[NSArray arrayWithObjects: "Label 1", 1, nil]];
[array addObject:[NSArray arrayWithObjects: "Label 2", 4, nil]];
[array addObject:[NSArray arrayWithObjects: "Label 3", 2, nil]];
[array addObject:[NSArray arrayWithObjects: "Label 4", 6, nil]];
[array addObject:[NSArray arrayWithObjects: "Label 5", 0, nil]];

First column contain a Label and 2nd one is a score I want the array to be sorted descending.

Is the way I am storing my data a good one ? Is there a better way to do this than using NSMutableArrays in NSMutableArray ?

I'm new to iPhone dev, I've seen some code about sorting but didn't feel good with that.

Thanks in advance for your answers !

+4  A: 

This would be much easier if you were to create a custom object (or at least use an NSDictionary) to store the information, instead of using an array.

For example:

//ScoreRecord.h
@interface ScoreRecord : NSObject {
  NSString * label;
  NSUInteger score;
}
@property (nonatomic, retain) NSString * label;
@property (nonatomic) NSUInteger score;
@end

//ScoreRecord.m
#import "ScoreRecord.h"
@implementation ScoreRecord 
@synthesize label, score;

- (void) dealloc {
  [score release];
  [super dealloc];
}

@end

//elsewhere:
NSMutableArray * scores = [[NSMutableArray alloc] init];
ScoreRecord * first = [[ScoreRecord alloc] init];
[first setLabel:@"Label 1"];
[first setScore:1];
[scores addObject:first];
[first release];
//...etc for the rest of your scores

Once you've populated your scores array, you can now do:

//the "key" is the *name* of the @property as a string.  So you can also sort by @"label" if you'd like
NSSortDescriptor * sortByScore = [NSSortDescriptor sortDescriptorWithKey:@"score" ascending:YES];
[scores sortUsingDescriptors:[NSArray arrayWithObject:sortByScore]];

After this, your scores array will be sorted by the score ascending.

Dave DeLong
Thanks a lot for your help !I created my own object, it is very easier than before to sort my data.Why do you use NSUInteger instead of simple int ?
Dough
@Dough `NSUInteger` will be 32 or 64 bit, depending on what platform it's running on. `int` will always be 32 bits, and are the preferred and recommended way of using integers.
Dave DeLong
+1  A: 

You don't need to create a custom class for something so trivial, it's a waste of code. You should use an array of NSDictionary's (dictionary in ObjC = hash in other languages).

Do it like this:

  NSMutableArray * array = [NSMutableArray arrayWithObjects:
                            [NSDictionary dictionaryWithObject:@"1" forKey:@"my_label"],
                            [NSDictionary dictionaryWithObject:@"2" forKey:@"my_label"],
                            [NSDictionary dictionaryWithObject:@"3" forKey:@"my_label"],
                            [NSDictionary dictionaryWithObject:@"4" forKey:@"my_label"],
                            [NSDictionary dictionaryWithObject:@"5" forKey:@"my_label"],
                            nil];
  NSSortDescriptor * sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"my_label" ascending:YES] autorelease];
  [array sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
sbwoodside
Thanks for this answer !I prefer to use my own object in my case, but I think this can be a good one too :D
Dough