[IOS]比较类内元素后重新排序

-(NSArray *)sortedDevicesByStrength:(NSMutableArray*)devices{

    NSArray<HwLanDevice*> *sortedArray = [devices sortedArrayUsingComparator:^(id obj1,id obj2){
        long val1 = ((HwLanDevice*)obj1).powerLevel;
        long val2 = ((HwLanDevice*)obj2).powerLevel;
        
        NSLog(@"%lu~%lu",val1,val2);
        //升序,假如需要降序的话,只需要修改下面的逻辑
        if (val1 < val2)
        {
            return NSOrderedAscending;
            
        }else{
            return NSOrderedDescending;
            
        }
    }];

    return sortedArray;
}

参考:

1.https://www.cnblogs.com/hello-LJ/p/4031471.html

2.http://blog.csdn.net/zfx5130/article/details/42433283

猜你喜欢

转载自jameskaron.iteye.com/blog/2414223