【发布时间】:2015-07-04 10:05:56
【问题描述】:
所以我有 NSDictionary 项的 NSMutableArray。每个 NSDictionary 项都有一个键“名称”,应该用于按字母顺序对数组进行排序。除了它的“排序”部分有效。它没有按确切的字母顺序排序。它按顺序移动了一些字典,但留下了一些乱序。
NSMutableArray *section1 = [champs lastObject];
NSArray *oldSection1 = [NSArray arrayWithArray:section1];
[section1 sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSDictionary *dictA = (NSDictionary *)obj1;
NSDictionary *dictB = (NSDictionary *)obj2;
NSString *champName1 = dictA[@"name"];
NSString *champName2 = dictB[@"name"];
return [champName1 compare:champName2];
}];
// Animation
for (NSDictionary *champInfo2 in section1) {
if ([oldSection1 indexOfObject:champInfo2] != [section1 indexOfObject:champInfo2]) {
[self.collectionView moveItemAtIndexPath:[NSIndexPath indexPathForItem:[oldSection1 indexOfObject:champInfo2] inSection:1] toIndexPath:[NSIndexPath indexPathForItem:[section1 indexOfObject:champInfo2] inSection:1]];
}
}
我什至试过这段代码
NSMutableArray *section1 = [champs lastObject];
NSArray *oldSection1 = [NSArray arrayWithArray:section1];
/*[section1 sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSDictionary *dictA = (NSDictionary *)obj1;
NSDictionary *dictB = (NSDictionary *)obj2;
NSString *champName1 = dictA[@"name"];
NSString *champName2 = dictB[@"name"];
return [champName1 compare:champName2];
}];*/
section1 = [[NSArray arrayWithArray:section1] sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]]].mutableCopy;
// Animation
for (NSDictionary *champInfo2 in section1) {
if ([oldSection1 indexOfObject:champInfo2] != [section1 indexOfObject:champInfo2]) {
[self.collectionView moveItemAtIndexPath:[NSIndexPath indexPathForItem:[oldSection1 indexOfObject:champInfo2] inSection:1] toIndexPath:[NSIndexPath indexPathForItem:[section1 indexOfObject:champInfo2] inSection:1]];
}
}
好的,问题是我没有在重组之前更新我的内容快照,因为我在视觉上更新了它。更新代码:
// Animation
for (NSDictionary *champInfo2 in section) {
if ([oldSection indexOfObject:champInfo2] != [section indexOfObject:champInfo2]) {
[self.collectionView moveItemAtIndexPath:[NSIndexPath indexPathForItem:[oldSection indexOfObject:champInfo2] inSection:[champs indexOfObject:section]] toIndexPath:[NSIndexPath indexPathForItem:[section indexOfObject:champInfo2] inSection:[champs indexOfObject:section]]];
[oldSection removeObject:champInfo2];
[oldSection insertObject:champInfo2 atIndex:[section indexOfObject:champInfo2]];
}
}
【问题讨论】:
标签: ios objective-c sorting