欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

iOS 对NSMutableArray进行排序和过滤的实例

时间:2021-05-15 09:08:30|栏目:iOS代码|点击:

根据实体类的属性进行排序,这个属性排序还可以增加几个排序规则,前后的规则先判断,如果一样的则根据第二个排序规则来判断

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中UIAlertView3秒后消失的两种实现方法

栏    目:iOS代码

下一篇:iOS中设置圆角的几种方法示例

本文标题:iOS 对NSMutableArray进行排序和过滤的实例

本文地址:http://www.codeinn.net/misctech/122213.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有