iOS开发中对NSArray或者NSMutableArray中的内容排序

it2024-12-21  6

原文引用至               http://blog.unieagle.net/2012/05/10/ios开发中对nsarray或者nsmutablearray中的内容排序/

 

NSMutableArray *feedsBuffer; //初始化buffer以及填充数据 //....... //排序只需要两句话:已针对数组内对象的publishTime属性(NSDate)排序为例: NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"publishTime" ascending:NO]; [feedsBuffer sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; //假如feedsBuffer是NSArray,那么有一个方法是 NSArray *sortedArray = [feedsBuffer sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; //如果排序的依据比较复杂,那么可以使用Block进行大小判断自定义: [feedsBuffer sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { //返回三者其一:NSOrderedAscending, NSOrderedSame, NSOrderedDescending return NSOrderedSame; }];

我的项目是要对未读消息进行靠前排序

- (void)reloadNuReadMsgCount { NSMutableDictionary *sessionDic = [[IMMsgDBAccess sharedInstance] loadAllSessions]; NSArray *allSession = [sessionDic allValues]; for (NSMutableDictionary *dic in _dataArray) { for ( ECSession* session in allSession) { NSString *sessionId = session.sessionId; NSString *ronggroupid = [dic objectForKey:@"ronggroupid"]; if ([sessionId isEqualToString:ronggroupid]) { [dic setValue:[NSString stringWithFormat:@"%ld", (long)session.unreadCount] forKey:MsgUnReadCount]; } } } // 根据字典里的 MsgUnReadCount 属性来排序未读消息 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"MsgUnReadCount" ascending:NO]; [_dataArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; NSLog(@"%@",_dataArray); [_contentTableView reloadData]; }

 

转载于:https://www.cnblogs.com/yulongjiayuan/p/5354568.html

相关资源:数据结构—成绩单生成器
最新回复(0)