Objective-C: Sort NSMutableArray containing NSMutableArrays

I am currently using NSMutableArrays in my designs to store some data taken from an HTTP servlet.

Everything is in order, 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]]; 

The first column contains the label, and the second contains the rating. I want the array to be sorted in descending order.

Am I storing my data so well? Is there a better way to do this than using NSMutableArrays in NSMutableArray ?

I am new to iPhone dev, I saw some sorting code, but didn't feel good.

Thanks in advance for your answers!

+6
sorting objective-c nsmutablearray
source share
2 answers

It would be much simpler if you created a custom object (or at least used an NSDictionary ) to store 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 { [label 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 have populated the scores array, you can do the following:

 //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 that, your scores array will be sorted in ascending order.

+10
source share

You do not need to create a custom class for something so trivial, it is a waste of code. You should use an NSDictionary array (a 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]]; 
+5
source share

All Articles