根據(jù)實(shí)體類(lèi)的屬性進(jìn)行排序,這個(gè)屬性排序還可以增加幾個(gè)排序規(guī)則,前后的規(guī)則先判斷,如果一樣的則根據(jù)第二個(gè)排序規(guī)則來(lái)判斷
我們提供的服務(wù)有:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、山城ssl等。為上千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的山城網(wǎng)站制作公司
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"data" ascending:YES];//其中,price為數(shù)組中的對(duì)象的屬性,這個(gè)針對(duì)數(shù)組中存放對(duì)象比較更簡(jiǎn)潔方便 NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1]; [self.dataArray sortUsingDescriptors:sortDescriptors];
過(guò)濾不會(huì)對(duì)原來(lái)的數(shù)據(jù)產(chǎn)生影響,而是生成符合過(guò)濾條件的NSArray數(shù)據(jù)
NSPredicate *apredicate=[NSPredicate predicateWithFormat:@"id>=%ld AND id<=%ld",3,5]; NSArray *newArr=[self.dataArray filteredArrayUsingPredicate:apredicate];
下面是個(gè)例子可以運(yùn)行看看
#import@interface TestBean : NSObject @property(nonatomic,assign)int id; @property(nonatomic,strong)NSString* data; @end
#import@interface ViewController : UIViewController @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為數(shù)組中的對(duì)象的屬性,這個(gè)針對(duì)數(shù)組中存放對(duì)象比較更簡(jiǎn)潔方便 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 對(duì)NSMutableArray進(jìn)行排序和過(guò)濾的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持創(chuàng)新互聯(lián)。