// // NSArray+RQExtension.m // XinShouJiaDao // // Created by 张嵘 on 2021/7/7. // Copyright © 2021 JCZ. All rights reserved. // #import "NSArray+RQExtension.h" @implementation NSArray (RQExtension) -(id)objAtPath:(NSIndexPath*)path { if (path.section < self.count ) { NSArray* subObj = self[path.section]; if([subObj isKindOfClass:[NSArray class]] || [subObj isKindOfClass:[NSMutableArray class]]) { if (path.row < subObj.count) { return [self[path.section] objectAtIndex:path.row]; } } } return nil; } /* * @brief 将数组随机打乱 */ - (NSArray *)rq_randomArray { // 转为可变数组 NSMutableArray * tmp = self.mutableCopy; // 获取数组长度 NSInteger count = tmp.count; // 开始循环 while (count > 0) { // 获取随机角标 NSInteger index = arc4random_uniform((int)(count - 1)); // 获取角标对应的值 id value = tmp[index]; // 交换数组元素位置 tmp[index] = tmp[count - 1]; tmp[count - 1] = value; count --; } // 返回打乱顺序之后的数组 return tmp.copy; } #pragma mark -- 数组排序方法(升序) - (NSArray *)arraySortASC { return [self sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { return [obj1 compare:obj2]; }]; } #pragma mark -- 数组排序方法(降序) - (NSArray *)arraySortDESC{ return [self sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { return [obj2 compare:obj1]; }]; } #pragma mark -- 数组排序方法(乱序) - (NSArray *)arraySortBreak { return [self sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { if (arc4random_uniform(2) == 0) { return [obj2 compare:obj1]; //降序 } else { return [obj1 compare:obj2]; //升序 } }]; } @end