Iphone NSMutableArray的订单、存储桶排序和订单存储桶
我有一个Iphone NSMutableArray的订单、存储桶排序和订单存储桶,iphone,objective-c,nsmutablearray,nsarray,key-value,Iphone,Objective C,Nsmutablearray,Nsarray,Key Value,我有一个NSArray的对象,它有一个有趣的属性,我希望以以下方式使用它:给定我的具有属性的对象数组: Object1-属性A; Object2-属性A; Object3-属性B; Object4-属性D; Object5-属性D; Object6-属性D 我希望这些数据按其属性排序到一个新数组中: Array1-Objects Object1、Object2 Array2-Objects对象3 Array3-Objects对象4、Object5、Object6 然后在每个数组中,使用timeS
NSArray
的对象
,它有一个有趣的属性
,我希望以以下方式使用它:给定我的具有属性的对象数组:
Object1-属性A
;
Object2-属性A
;
Object3-属性B
;
Object4-属性D
;
Object5-属性D
;
Object6-属性D
我希望这些数据按其属性排序到一个新数组中:
Array1-Objects Object1、Object2
Array2-Objects对象3
Array3-Objects对象4、Object5、Object6
然后在每个数组中,使用timeStamp
属性进行排序
我尝试通过创建一个字典来实现这一点,通过属性将有趣的对象添加到字典中,如if([dictionary objectForKey:@“propertyVal]”)//add object else//create array for key,add object to array
。这种方法没有按预期工作,因为我最终需要使用allKeysForValue
对NSMutableDictionary
进行解键,这是不可靠的
我觉得这是一个相当普遍的问题,我很想听到关于如何解决这个问题的任何见解。代码很棒,但即使是一个算法(使用适当的对象)也应该足够了。它不是一个适当的bucket排序,但应该适用于一组三个属性。稍加修改,您应该能够针对任意数量的属性进行调整: 编辑。我制作了一个动态版本(只需将属性类型设置为您需要的):
-(NSMutableArray*)顺序:(NSDictionary*)对象byProperty:(id)属性{
NSMutableSet*propertySet=[NSMutableSet-setWithCapacity:5];//因此我们可以计算唯一的属性
对于(对象*obj在[对象所有值]){
[propertySet addObject:[obj property]];
}
NSMutableArray*objectCollections=[NSMutableArray阵列容量:[propertySet count]];
//为每个属性创建数组
对于(int i=0;i<[objects allValues];i++){
NSMutableArray*集合=[NSMutableArray阵列容量:5];
[objectCollections添加对象:集合];
}
NSArray*allProperties=[propertySet AllObject];
//根据特定属性将对象推入数组
对于(对象*obj在[dictionary allValues]中){
[[objectCollections objectAtIndex:[allProperties indexOfObject:[obj property]]addObject:obj];
}
NSMutableArray*结果=[NSMutableArray阵列容量:[objectCollections计数]];
//按时间戳对数组排序
对于(int i=0;i<[objectCollections计数];i++){
[结果添加对象:[[objectCollections objectAtIndex:i]使用比较器进行排序:^(id obj1,id obj2){
如果([(对象*)obj1时间戳]>[(对象*)obj2时间戳]){
返回(NSComparisonResult)或删除;
}
如果([(对象*)obj1时间戳]<[(对象*)obj2时间戳]){
返回(NSComparisonResult)或撤销;
}
返回(NSComparisonResult)命令名;
}];
}
返回结果;
}
使用数组字典听起来很合理。请解释“为NSMutableDictionary解键”的含义、需要这样做的原因以及失败的原因。此外,没有-[NSDictionary allKeysForValue]
,你的意思是allKeysForObject
?是的,我的意思是allKeysForObject
。当我传入原始字符串时,它返回nil
。再次解释一下“为NSMutableDictionary解键”对你意味着什么,因为它不清楚。当你构建完字典后,你应该能够遍历键和值(数组)非常容易,无需-allKeysForObject
。问题是创建数组“提前”不可行,因为我不一定知道属性A、B、C…将是什么。它们可以是任何东西。好吧,然后将所有东西包装在函数中并使其动态化。您可以使用我的解决方案的常规模式并对其进行参数化。[\u NSCFString beginDate]:当我尝试进行比较时,如果([(WSConference*)obj1 beginDate]>[(WSConference*)obj2 beginDate]),则发送到实例0x69762c0的选择器无法识别
…我正在使用ARC,beginDate
是一个强大的、非原子的
@属性
,它已经解决了,但仍然失败…我将尝试进一步调试。你的方法看起来很有希望我用一个更好的版本编辑了我的帖子。对于你最后的评论,你似乎无法提取beginDate属性从你的物体中提取的能量(你合成了吗?)
- (NSMutableArray *)order:(NSDictionary *)objects byProperty:(id)property {
NSMutableSet *propertySet = [NSMutableSet setWithCapacity:5]; // so we can count the unique properties
for (Object *obj in [objects allValues]) {
[propertySet addObject:[obj property]];
}
NSMutableArray *objectCollections = [NSMutableArray arrayWithCapacity:[propertySet count]];
// create arrays for every property
for (int i = 0; i < [objects allValues]; i++) {
NSMutableArray *collection = [NSMutableArray arrayWithCapacity:5];
[objectCollections addObject:collection];
}
NSArray *allProperties = [propertySet allObjects];
// push objects into arrays according to a certain property
for (Object *obj in [dictionary allValues]) {
[[objectCollections objectAtIndex:[allProperties indexOfObject:[obj property]] addObject:obj];
}
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[objectCollections count]];
// sort arrays by timestamp
for (int i = 0; i < [objectCollections count]; i++) {
[result addObject:[[objectCollections objectAtIndex:i] sortedArrayUsingComparator:^(id obj1, id obj2) {
if ([(Object *)obj1 timeStamp] > [(Object *)obj2 timeStamp]) {
return (NSComparisonResult)NSOrderedAscending;
}
if ([(Object *)obj1 timeStamp] < [(Object *)obj2 timeStamp]) {
return (NSComparisonResult)NSOrderedDescending;
}
return (NSComparisonResult)NSOrderedSame;
}];
}
return result;
}