我使用此代码
创建了一个Object的NSMutableArray- (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];
}
}
这是我的DataModel类
.H文件
#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文件
#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
现在我想根据我看到的那个数组中的名字对它进行排序this问题在SO中看起来像我的并且使用它的答案代码来解决我的问题但它对我来说不起作用这个
[tableAry sortUsingDescriptors:
[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)]]];
NSLog(@"tableAry : %@",tableAry);
那么我怎样才能对数组进行排序
更新
作为@Martin R和@Rick说我已经分配了我的数组,但现在我收到了这个错误。
***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [DataModel caseInsensitiveCompare:]:无法识别的选择器发送到实例0x7550850'
答案 0 :(得分:3)
您也可以使用NSSortDescriptor
。
NSSortDescriptor* sortDes = [[NSSortDescriptor alloc] initWithKey:@"your key" ascending:YES];
[_array sortUsingDescriptors:[NSArray arrayWithObject:sortDes]];
试试吧。
答案 1 :(得分:2)
[tableAry sortUsingComparator:^NSComparisonResult(DataModel *obj1, DataModel *obj2) {
return [obj1.name caseInsensitiveCompare:obj2.name];
}];
答案 2 :(得分:1)
-(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;
}
答案 3 :(得分:0)
-(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;
}
注意:您可以在卡片符号
的位置使用您的标签或其他任何东西