iOS中数组对象按照对象的一个字段进行排序

NSMutableArray *commentListArray=[[NSMutableArray alloca]init];


// 为每个属性创建NSSortDescriptor对象,并确定为升序

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"commentId" ascending:YES];//

// 设置排序优先级,并组成数组。这里优先级最高为commentId

NSArray * sortDescriptors =  [[NSArray alloc] initWithObjects:&sortDescriptor count:1];

[commentListArray sortUsingDescriptors:sortDescriptors];

// 为数组中每个元素执行方法,输出状态

[commentListArray makeObjectsPerformSelector:@selector(printState)];

你可能感兴趣的:(iOS中数组对象按照对象的一个字段进行排序)