iOS sort array 数组排序里面的对象
一:如下代码
//对数据按次数排序 NSArray *sortArrays = [pinCiDataArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { NSNumber *num1 = [(ReplayModel *)obj1 useTimes]; NSNumber *num2 = [(ReplayModel *)obj2 useTimes]; return [num2 compare:num1]; }];
数组pinCiDataArray保存的是对象 ReplayModel;
ReplayModel 对象有一个 useTimes的 NsNumber属性;
如上代码对数组按 useTimes的使用次数排序;
[num1 compare :num2] 为结果升序
[num2 compare :num1] 为结果降序
参考:http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it