4

我使用此代码创建了一个对象的 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

现在我想根据该数组中的名称对其进行排序,我在 SO 中看到了这个问题,它看起来像我的,并使用它的答案代码来解决我的问题,但它对我不起作用,就是这样

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

    NSLog(@"tableAry : %@",tableAry);

那么我怎样才能对我的数组进行排序

更新

正如@Martin R 和@Rick 所说,我已经分配了我的数组,但现在我得到了这个错误。

*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[DataModel caseInsensitiveCompare:]:无法识别的选择器发送到实例 0x7550850”

4

4 回答 4

3

您也可以使用NSSortDescriptor.

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

试试看。

于 2013-07-20T16:38:24.117 回答
2
[tableAry sortUsingComparator:^NSComparisonResult(DataModel *obj1, DataModel *obj2) {
        return [obj1.name caseInsensitiveCompare:obj2.name];
}];
于 2013-07-20T15:53:12.133 回答
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;

}

于 2013-12-26T09:47:49.180 回答
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;
}

注意:您可以在 cardSymbol 的位置使用您的标签或其他任何东西

于 2013-11-23T14:58:52.573 回答