iOS 對NSMutableArray進(jìn)行排序和過濾的實例
根據(jù)實體類的屬性進(jìn)行排序,這個屬性排序還可以增加幾個排序規(guī)則,前后的規(guī)則先判斷,如果一樣的則根據(jù)第二個排序規(guī)則來判斷
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"data" ascending:YES];//其中,price為數(shù)組中的對象的屬性,這個針對數(shù)組中存放對象比較更簡潔方便 NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1]; [self.dataArray sortUsingDescriptors:sortDescriptors];
過濾不會對原來的數(shù)據(jù)產(chǎn)生影響,而是生成符合過濾條件的NSArray數(shù)據(jù)
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為數(shù)組中的對象的屬性,這個針對數(shù)組中存放對象比較更簡潔方便 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進(jìn)行排序和過濾的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
iOS App開發(fā)中UITextField組件的常用屬性小結(jié)
這篇文章主要介紹了iOS App開發(fā)中UITextField組件的常用屬性小結(jié),文中還介紹了UITextField隱藏鍵盤及為內(nèi)容增加校驗的兩個使用技巧,需要的朋友可以參考下2016-04-04iOS在頁面銷毀時如何優(yōu)雅的cancel網(wǎng)絡(luò)請求詳解
這篇文章主要給大家介紹了關(guān)于iOS在頁面銷毀時如何優(yōu)雅的cancel網(wǎng)絡(luò)請求的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05iOS的UI開發(fā)中UITabBarControlle的基本使用教程
這篇文章主要介紹了iOS的UI開發(fā)中UITabBarControlle的基本使用教程,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12IOS 遠(yuǎn)程通知兼容(IOS7,IOS8)實例詳解
這篇文章主要介紹了IOS 遠(yuǎn)程通知兼容(IOS7,IOS8)實例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03