数组中的元素是字典,字典中的某一个元素,比如说姓名,现在需要按照姓名的首字母来排序,怎么搞?
做法很简单,在字典中加一个元素,保存姓名的首字母,然后用下面的方法排序。
- (void)sortWifiList {
NSArray *resultArray = [myDataArray sortedArrayUsingFunction:compare context:NULL];
[myDataArray removeAllObjects];
[myDataArray addObjectsFromArray:resultArray];
}
NSComparisonResult compare(NSDictionary *firstDict, NSDictionary *secondDict, void *context) {
if ([[firstDict objectForKey:@"SSIDSortKey"] intValue] < [[secondDict objectForKey:@"SSIDSortKey"] intValue])
return NSOrderedAscending;
else if ([[firstDict objectForKey:@"SSIDSortKey"] intValue] > [[secondDict objectForKey:@"SSIDSortKey"] intValue])
return NSOrderedDescending;
else
return NSOrderedSame;
}