[IOS] Reorder after comparing elements within a class

-(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);
        //Ascending order, if you need descending order, you only need to modify the following logic
        if (val1 < val2)
        {
            return NSOrderedAscending;
            
        }else{
            return NSOrderedDescending;
            
        }
    }];

    return sortedArray;
}

 

refer to:

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

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326012862&siteId=291194637
Recommended