iphone 对NSMutableArray数组排序

来源:互联网 发布:广州数控车床仿真软件 编辑:程序博客网 时间:2024/05/06 02:50
自定义一个类,并初始化该类,然后添加到NSMutableArray数组中,并对数组进行排序。


1.结构定义:
//每一个房态的状态
@interface RoomStatus : NSObject
{
double _price;
}
@property (nonatomic) double price;
@end


@implementation DXRoomStatus
@synthesize price = _price;


- (void)dealloc
{
[super dealloc];
}
@end




2.在.h文件中定义一个数组
NSMutableArray * _roomStatus;




3.初始化数组。
//初始化数组
for(int i=0 ; i<4; i++){
RoomStatus * status = [[RoomStatus alloc] init];
status.price = 4*13;

[_roomStatus addObject:status];
}


//排序,根据_price字段升序排序
NSSortDescriptor* sortByA = [NSSortDescriptor sortDescriptorWithKey:@"_price" ascending:YES];
[_roomStatus sortUsingDescriptors:[NSArray arrayWithObject:sortByA]];