NSSortDescriptor 的使用


 NSSortDescriptor  是什么 ?

 你可以將它看做是對一個排序規則的描述者  因為我們可以使用它來對我們數組中的對象進行排序操作

 假設現在有這樣一個需求: 數組里面有十個Person對象 每一個Person對象有自己的名字年齡和分數  我們需要對數組里面的這十個對象做排序操作 規則如下:

  1.首先按照年齡排序

  2.如果年齡相同按照分數排序

  要實現這個需求 如果你之前 不知道NSSortDescriptor 這個對象 你可能會寫很多的代碼 現在借助這個對象我們會非常容易的實現上面的需求

 下面是實例:

 首先我們需要創建一個Person類: 包括姓名 年齡 分數三個屬性  同時提供一個構造方法 用於快速創建一個Person對象 代碼如下:

 

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property(nonatomic,strong)NSString *name;

@property(nonatomic,assign)NSInteger age;

@property(nonatomic,assign)float  score;

- (instancetype)initWithName:(NSString*)name age:(NSInteger)age score:(float)score;

@end
#import "Person.h"

@implementation Person

- (instancetype)initWithName:(NSString *)name age:(NSInteger)age score:(float)score
{
    if (self = [super init]) {
        
        self.name = name;
        self.age = age;
        self.score = score;
        
    }
    return self;
}

@end

 

接下來我們在 ViewController(ViewController繼承自UITableviewController 並且程序的跟控制器是一個導航控制器)然后做如下的幾件事情:

 1. 創建十個Person對象 並且存入datas數組中

 2.將數據用TableView展示

 3.設置導航欄左邊的按鈕為排序 點擊排序 可以按照我們設定的規則進行排序

 

   創建十個Person對象 並且存入datas數組中 我們給ViewController 增加一個數組屬性 datas 強引用着

   

@interface ViewController ()
@property(nonatomic,strong)NSMutableArray *datas;
@end

  然后對datas采取懶加載的方式:

  

- (NSMutableArray *)datas
{
    if (!_datas) {
        
        _datas = [NSMutableArray array];
        
        Person *p1 = [[Person alloc] initWithName:@"jack" age:20 score:97];
        Person *p2 = [[Person alloc] initWithName:@"anne" age:8 score:33];
        Person *p3 = [[Person alloc] initWithName:@"zhng" age:54 score:11];
        Person *p4 = [[Person alloc] initWithName:@"tuoma" age:76 score:54];
        Person *p5 = [[Person alloc] initWithName:@"gril" age:95 score:12];
        Person *p6 = [[Person alloc] initWithName:@"boy" age:21 score:76];
        Person *p7 = [[Person alloc] initWithName:@"big" age:53 score:98];
        Person *p8 = [[Person alloc] initWithName:@"hack" age:33 score:66];
        Person *p9 = [[Person alloc] initWithName:@"zoom" age:33 score:21];
        Person *p10 = [[Person alloc] initWithName:@"right" age:69 score:88];

        
        
        [_datas addObject:p1];
        [_datas addObject:p2];
        [_datas addObject:p3];
        [_datas addObject:p4];
        [_datas addObject:p5];
        [_datas addObject:p6];
        [_datas addObject:p7];
        [_datas addObject:p8];
        [_datas addObject:p9];
        [_datas addObject:p10];


    }
    return _datas;
}

 接下來我們先把這些數據展示出來 在ViewController里面寫上如下代碼:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.datas.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    
    Person *p = self.datas[indexPath.row];
    
    cell.textLabel.text = [NSString stringWithFormat:@"姓名:%@  年齡:%zd",p.name,p.age];
    cell.detailTextLabel.text =[NSString stringWithFormat:@"%f", p.score];

    return cell;
}

 

 接下來我們可以運行一下看看界面效果:

接下來我們需要進行排序:

 規則如下:

  1.首先按照年齡排序

  2.如果年齡相同按照分數排序

那么我們需要創建排序描述者,一個描述着只能對一個屬性進行描述 如果需要描述多個 我們需要創建多個描述者

我們這里的需求就需要創建兩個描述者  一個是對年齡描述 一個是對分數描述 代碼如下:

NSSortDescriptor *ageSD = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];//ascending:YES 代表升序 如果為NO 代表降序
NSSortDescriptor *scoreSD=[NSSortDescriptor sortDescriptorWithKey:@"score" ascending:YES];

創建好這兩個描述者之后 我們就可以調用數組的 sortedArrayUsingDescriptors 方法來實現排序

sortedArrayUsingDescriptors方法接收一個數組的參數 里面放描述者 然后他會返回一個排序好的數組 所以我們這樣做:

self.datas = [[self.datas sortedArrayUsingDescriptors:@[ageSD,scoreSD]] mutableCopy];

我們點擊導航欄左邊的排序按鈕的時候 會執行以下操作:

- (IBAction)sortAge:(id)sender {
    
    NSSortDescriptor *ageSD = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
    NSSortDescriptor *scoreSD=[NSSortDescriptor sortDescriptorWithKey:@"score" ascending:YES];
    
    self.datas = [[self.datas sortedArrayUsingDescriptors:@[ageSD,scoreSD]] mutableCopy];
    
    [self.tableView reloadData];
    
    
}

 

這個時候 我們再看下效果:

可以看到 如果年齡相同 我們就按照分數進行排序  這樣我們的這個排序就完成了 如果你有多個排序需求 你就創建多個排序描述者就可以了 是不是很簡單

 

 

 

 

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM