How to sort an NSMutableArray object in ascending or descending order

I created an NSMutableArray 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 this array, I saw this Question in SO, which is similar to mine and uses its response code to solve my problem but for me it did not work, that it

 [tableAry sortUsingDescriptors: [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)]]]; NSLog(@"tableAry : %@",tableAry); 

So how can I sort the array

Update

Like @Martin R And @Rick said I allocated my array, but now I got this error.

*** Application termination due to an uncaught exception "NSInvalidArgumentException", reason: '- [DataModel caseInsensitiveCompare:]: unrecognized selector sent to instance 0x7550850

+4
source share
4 answers

You can also use NSSortDescriptor .

 NSSortDescriptor* sortDes = [[NSSortDescriptor alloc] initWithKey:@"your key" ascending:YES]; [_array sortUsingDescriptors:[NSArray arrayWithObject:sortDes]]; 

Give it a try.

+3
source
 [tableAry sortUsingComparator:^NSComparisonResult(DataModel *obj1, DataModel *obj2) { return [obj1.name caseInsensitiveCompare:obj2.name]; }]; 
+2
source
 -(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; 

}

+1
source
 -(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 other thing in the place of the map. Symbol

0
source

Source: https://habr.com/ru/post/1492476/


All Articles