iOS 对NSMutableArray进行排序和过滤的实例
2019/7/9 22:44:33
本文主要是介绍iOS 对NSMutableArray进行排序和过滤的实例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
根据实体类的属性进行排序,这个属性排序还可以增加几个排序规则,前后的规则先判断,如果一样的则根据第二个排序规则来判断
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"data" ascending:YES];//其中,price为数组中的对象的属性,这个针对数组中存放对象比较更简洁方便 NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1]; [self.dataArray sortUsingDescriptors:sortDescriptors];
过滤不会对原来的数据产生影响,而是生成符合过滤条件的NSArray数据
NSPredicate *apredicate=[NSPredicate predicateWithFormat:@"id>=%ld AND id<=%ld",3,5]; NSArray *newArr=[self.dataArray filteredArrayUsingPredicate:apredicate];
下面是个例子可以运行看看
#import <Foundation/Foundation.h> @interface TestBean : NSObject @property(nonatomic,assign)int id; @property(nonatomic,strong)NSString* data; @end
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> @property (strong, nonatomic) NSArray *list; @end // // ViewController.m // First // // Created by shanreal-iOS on 17/10/16. // Copyright © 2017年 shanreal.LongZhenHao. All rights reserved. // #import "ViewController.h" #import "TestBean.h" @interface ViewController () @property(nonatomic,strong)NSMutableArray* dataArray; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _dataArray = [NSMutableArray array]; TestBean* one = [[TestBean alloc]init]; one.id = 1; one.data = @"one"; [_dataArray addObject:one]; one = [[TestBean alloc]init]; one.id = 2; one.data = @"two"; [_dataArray addObject:one]; one = [[TestBean alloc]init]; one.id = 22; one.data = @"two2"; [_dataArray addObject:one]; one = [[TestBean alloc]init]; one.id = 3; one.data = @"three"; [_dataArray addObject:one]; one = [[TestBean alloc]init]; one.id = 4; one.data = @"four"; [_dataArray addObject:one]; one = [[TestBean alloc]init]; one.id = 44; one.data = @"four4"; [_dataArray addObject:one]; one = [[TestBean alloc]init]; one.id = 5; one.data = @"five"; [_dataArray addObject:one]; one = [[TestBean alloc]init]; one.id = 6; one.data = @"six"; [_dataArray addObject:one]; [self oneClick]; [self twoClick]; } -(void)oneClick{ NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"data" ascending:YES];//其中,price为数组中的对象的属性,这个针对数组中存放对象比较更简洁方便 NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1]; [self.dataArray sortUsingDescriptors:sortDescriptors]; [self logArray:self.dataArray]; } -(void)twoClick{ NSPredicate *apredicate=[NSPredicate predicateWithFormat:@"id>=%ld AND id<=%ld",3,5]; NSArray *newArr=[self.dataArray filteredArrayUsingPredicate:apredicate]; [self logArray:newArr]; } -(void)logArray:(NSArray*)array{ NSLog(@"---------------------------------"); for(TestBean* bean in array){ NSLog(@"%d %@",bean.id,bean.data); } } @end
以上这篇iOS 对NSMutableArray进行排序和过滤的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持找一找教程网。
这篇关于iOS 对NSMutableArray进行排序和过滤的实例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-12Axios库资料:新手入门必读教程
- 2024-11-11Axios库项目实战:新手入门教程
- 2024-09-29Axios库教程:初学者必备指南
- 2024-08-29Axios库资料:新手入门指南与基本使用教程
- 2024-03-14system bios shadowed
- 2024-03-14gabios
- 2024-02-07iOS应用提交上架的最新流程
- 2024-02-06打包 iOS 的 IPA 文件
- 2023-12-07uniapp打包iOS应用并通过审核:代码混淆的终极解决方案 ?
- 2023-11-25uniapp IOS从打包到上架流程(详细简单) 原创