zoukankan      html  css  js  c++  java
  • NSSortDescriptor(数组排序)

    如果数组里面的每一个元素都是一个个model,例如

    DepartsDate.h文件

    [plain] view plaincopy
    1. #import <Foundation/Foundation.h>  
    2.   
    3. @interface DepartsDate : NSObject  
    4.   
    5. @property (nonatomic, retain) NSDate *date;  
    6. @property (nonatomic, assign) int    price;  
    7.   
    8. @end  
    DepartsDate.m文件

    [plain] view plaincopy
    1. #import "DepartsDate.h"  
    2.   
    3. @implementation DepartsDate  
    4.   
    5. @synthesize date, price;  
    6.   
    7. - (void)dealloc  
    8. {  
    9.     [date release];  
    10.     [super dealloc];  
    11. }  
    12.   
    13. @end  
    那么对这个数组排序就可以这样

    [plain] view plaincopy
    1. NSMutableArray *array = [NSMutableArray array];  
    2. ......  
    3. NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES];[array sortUsingDescriptors:[NSArray arrayWithObject:sort]];  
    这是按照时间升序排列。如果需要降序,那么将YES改为NO。


    使用NSSortDescriptor可以很方便的进行多条件排序,例如:同样是上面的假设,首先按照价格升序排列;当价格相同时,按照日期降序排列。

    [plain] view plaincopy
    1. NSMutableArray *array = [NSMutableArray array];  
    2. ......  
    3. NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"price" ascending:YES];  
    4. NSSortDescriptor *sort2 = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO];  
    5. [array sortUsingDescriptors:[NSArray arrayWithObjects:sort1, sort2, nil]];  

  • 相关阅读:
    Knight Moves
    Knight Moves
    Catch him
    Catch him
    Linux查看硬件信息以及驱动设备的命令
    23种设计模式彩图
    Android开发指南-框架主题-安全和许可
    Android启动组件的三种主流及若干非主流方式
    ACE在Linux下编译安装
    void及void指针含义的深刻解析
  • 原文地址:https://www.cnblogs.com/lixingle/p/3707692.html
Copyright © 2011-2022 走看看