Reputation:
I have created an NSMutableArray of Object using this code
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray * ary1 = [NSArray arrayWithObjects:@"01/07",@"02/07",@"03/07",@"04/07",@"05/07",@"06/07",@"07/07", nil];
NSArray * ary2 = [NSArray arrayWithObjects:@"First",@"Second",@"Third",@"Forth",@"Fifth",@"Sixth",@"Seventh", nil];
NSArray * ary3 = [NSArray arrayWithObjects:@"1000",@"2000",@"3000",@"4000",@"5000",@"6000",@"7000", nil];
tableAry = [[NSMutableArray alloc] init];
for (int i=0; i<ary1.count; i++) {
//cardSummry will hold the data and give back the model to store in array and we can find that value using model
DataModel *dataModel = [[DataModel alloc] init];
dataModel.date = [ary1 objectAtIndex:i];
dataModel.name = [ary2 objectAtIndex:i];
dataModel.ammount = [ary3 objectAtIndex:i];
[tableAry addObject:dataModel];
}
}
And this is my DataModel Class
.H file
#import <Foundation/Foundation.h>
@interface DataModel : NSObject
//this variable is used to get the data from array
@property (nonatomic,strong) NSString *date;
@property (nonatomic,strong) NSString *name;
@property (nonatomic,strong) NSString *ammount;
//this method will genarate a data model which will be added to array for future use
+ (id)cardSummary:(NSString*)date name:(NSString*)name ammount:(NSString*)ammount;
@end
.M file
#import "DataModel.h"
@implementation DataModel
@synthesize date,name,ammount;
//this method will genarate a data model which will be added to array for future use
+ (id)cardSummary:(NSString*)date name:(NSString*)name ammount:(NSString*)ammount
{
DataModel *dataModel = [[self alloc] init];
[dataModel setDate:date];
[dataModel setAmmount:ammount];
[dataModel setName:name];
return dataModel;
}
@end
Now i want to sort it according to the name in that array i have seen this Question in SO which look like mine and use its answer code to solve my problem but it didn't work for me which is this
[tableAry sortUsingDescriptors:
[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)]]];
NSLog(@"tableAry : %@",tableAry);
So how can i sort my array
Update
As @Martin R And @Rick said i have alloc my array but now i got this error.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DataModel caseInsensitiveCompare:]: unrecognized selector sent to instance 0x7550850'
Upvotes: 4
Views: 5334
Reputation: 9944
[tableAry sortUsingComparator:^NSComparisonResult(DataModel *obj1, DataModel *obj2) {
return [obj1.name caseInsensitiveCompare:obj2.name];
}];
Upvotes: 2
Reputation: 25978
-(NSMutableArray*)sortArrayInAssendingOrder:(NSMutableArray*)array{ // array must have numerical value
NSMutableArray *newArray=[NSMutableArray arrayWithArray: array];
NSMutableArray *shortedArray=[[NSMutableArray alloc]init];
for (int i=0; i<=[newArray count]+1; i++) {
NSInteger value_1=[[newArray objectAtIndex:0]integerValue];
for(int j=0; j<[newArray count]; j++){
NSInteger value_2=[[newArray objectAtIndex:j]integerValue];
if(value_1>value_2){
value_1 =nil;
value_1 = value_2;
}
}
[shortedArray addObject:value_1];
[newArray removeObject: value_1];
}
[shortedArray addObject:[newArray objectAtIndex:0]];
return shortedArray;
}
Upvotes: 1
Reputation: 25978
-(NSMutableArray*)shortCardInAssendingOrder:(NSMutableArray*)cardSetArr{
NSMutableArray *shortedArray=[[NSMutableArray alloc]init];
for (int i=0; i<=[cardSetArr count]+1; i++) {
Card *firstCard=[cardSetArr objectAtIndex:0];
for(int j=0; j<[cardSetArr count]; j++){
Card *nextCard=[cardSetArr objectAtIndex:j];
if(firstCard.cardSymbol>nextCard.cardSymbol){
firstCard=nil;
firstCard=nextCard;
}
}
[shortedArray addObject:firstCard];
[cardSetArr removeObject:firstCard];
}
[shortedArray addObject:[cardSetArr objectAtIndex:0]];
return shortedArray;
}
Note: you can use ur tag or any thing else at tha place of cardSymbol
Upvotes: 0
Reputation: 273
You can also use the NSSortDescriptor
.
NSSortDescriptor* sortDes = [[NSSortDescriptor alloc] initWithKey:@"your key" ascending:YES];
[_array sortUsingDescriptors:[NSArray arrayWithObject:sortDes]];
Try it.
Upvotes: 3