现在的位置: 首页 > 综合 > 正文

利用 NSSortDescriptor 对数组进行排序

2013年01月03日 ⁄ 综合 ⁄ 共 583字 ⁄ 字号 评论关闭

 例如常用的方法:

将数组按key 排序 yesorno  为是否按顺序还是逆序

+ (void) changeArray:(NSMutableArray *)dicArray orderWithKey:(NSString *)key ascending:(BOOL)yesOrNo{

        NSSortDescriptor *distanceDescriptor = [[NSSortDescriptor alloc] initWithKey:key ascending:yesOrNo];

    
NSArray *descriptors = [NSArray arrayWithObjects:distanceDescriptor,nil];

    
[dicArray sortUsingDescriptors:descriptors];

     
  [distanceDescriptor release];

   
}

  NSSortDescriptor *bySpell  = [NSSortDescriptor sortDescriptorWithKey:@"spell" ascending:YES];

    _cities = [[NSArray arrayWithContentsOfFile:cityPlistPath] sortedArrayUsingDescriptors:@[bySpell]];

根据spell按照顺序排序。

抱歉!评论已关闭.