删除数组元素并将它们添加到相同的索引iOS

时间:2016-05-13 10:18:07

标签: ios objective-c arrays nsmutablearray nsmutabledictionary

我正在排序一个数组。 数组中有三种类型的元素。 特色 2.有机和 声称。

其中,我想只对有机元素进行排序,并将特色和声明的元素保存在自己的索引中。

下面是我的代码,我在字典中提取声明的和特色索引,因为键是索引,值是数组元素。

//Initialization
NSMutableArray *sortedArray = nil;
NSMutableDictionary *tempFeaturedDictionary = [[NSMutableDictionary alloc]init];
NSMutableDictionary *tempClaimedDictionary = [[NSMutableDictionary alloc]init];
NSMutableArray *tempOrganicArray = [[NSMutableArray alloc]init];

for (int i = 0; i < array.count; i++) {

    DRListing *isFeaturedObj = (DRListing*)[array objectAtIndex:i];

    if (isFeaturedObj.featured) {
        [tempFeaturedDictionary setObject:isFeaturedObj forKey:[@(i)stringValue]];
    }else if (isFeaturedObj.claimed)
    {
        [tempClaimedDictionary setObject:isFeaturedObj forKey:[@(i)stringValue]];
    }else
        [tempOrganicArray addObject:isFeaturedObj];

}

我再次将声明和特色添加回原始索引后排序为:

 sortedArray = [NSMutableArray arrayWithArray:[tempOrganicArray sortedArrayUsingDescriptors:sortDescriptorsArray]];

for (int i = 0; i<sortedArray.count; i++) {
    for (NSString *key in tempFeaturedDictionary) {
        if ( [[@(i)stringValue] isEqualToString: key] ) {
            [sortedArray insertObject:[tempFeaturedDictionary objectForKey:[@(i)stringValue]] atIndex:i];
        }}

    for (NSString *key in tempClaimedDictionary) {
        if ([[@(i)stringValue]isEqualToString:key ]) {
            [sortedArray insertObject:[tempClaimedDictionary objectForKey:[@(i)stringValue]] atIndex:i];
        }
    }
}

代码很好用。除了在'array'的最后一个索引处声明/(和)特色元素。因为'sortedArray'索引在这种情况下仍然小于'array.count'。 提前谢谢。

更新 -

我收到类型的响应数组:

[{featured1 featured2}, {organic1, organic2..}, {claimed1}, {featured11, featured12}, {organic11, organic12..}, {claimed2}, ..]

我可以只对这个数组中的有机元素进行排序。特色和声明不应该失去原来的索引位置。

3 个答案:

答案 0 :(得分:1)

我会迭代数组,提取有机物进行排序。然后对有机数组进行排序。然后迭代原始数组,从适当的角度获取原始数组中的元素或排序的有机数组中的元素。

NSMutableArray *organicsArray = [NSMutableArray new];

for (int i = 0; i < array.count; i++) {
    DRListing *isFeaturedObj = (DRListing*)array[i];

    if ((!isFeaturedObj.featured) && (!isFeaturedObj.claimed)) {
        [organicsArray addObject:isFeaturedObj];
    }
} 
NSMutableArray *sortedOrganicsArray = [[organicsArray sortedArrayUsingDescriptors:sortDescriptorsArray] mutableCopy];

NSMutableArray *outputArray = [NSMutableArray new];

for (int i = 0; i < array.count; i++) {
    DRListing *isFeaturedObj = (DRListing*)array[i];

    if ((!isFeaturedObj.featured) && (!isFeaturedObj.claimed)) {
        [outputArray addObject:sortedOrganicsArray[0]];
        [sortedOrganicsArray removeObjectAtIndex:0];
    } else {
        [outputArray addObject:isFeaturedObject];
    }
}

如果您颠倒了有机数组的排序顺序,那么你可能会提高效率

[outputArray addObject:[sortedOrganicsArray lastObject]];
[sortedOrganicsArray removeLastObject];

但如果您的阵列不是特别大,那么性能提升可能会微不足道。

答案 1 :(得分:0)

也许这是另一种选择:

NSMutableArray *organics = [NSMutableArray new];
NSMutableArray *others = [NSMutableArray new];

for (DRListing *isFeaturedObj in array) {
     if (isFeaturedObj.organic) {
         [organics addObject:isFeaturedObj];
     } else {
         [others addObject:isFeaturedObj];
     }
 }

 NSMutableArray *sorted = [NSMutableArray alloc]initWithObjects:organics,others, nil];

答案 2 :(得分:0)

您可以使用前2个功能。其他是我用来测试的。

- (DRListing *)getNextObjectFromArray:(NSArray *)array WithStartingIndex:(int)index
{
    for (int i=index; i<array.count; i++) {
        DRListing *obj = (DRListing*)[array objectAtIndex:i];
        if (!obj.featured && !obj.claimed)
        {
            return obj;
        }
    }

    return nil;
}

- (void)sortArray:(NSMutableArray *)array
{
    for (int pass = 0; pass<array.count-1; pass++) {
        for (int i=0; i<array.count-1; i++) {
            DRListing *obj = [self getNextObjectFromArray:array WithStartingIndex:i];
            int foundIndex = (int)[array indexOfObject:obj];
            DRListing *obj2 = [self getNextObjectFromArray:array WithStartingIndex:foundIndex+1];
            int foundIndex2 = (int)[array indexOfObject:obj2];

            if (obj!=nil && obj2 !=nil) {
                if (obj.value >= obj2.value) {
                    [array exchangeObjectAtIndex:foundIndex withObjectAtIndex:foundIndex2];
                }

                i = foundIndex;
            }
        }
    }

    NSLog(@"Sorted Data: %@",array);
}

- (NSMutableArray *)testData
{
    NSMutableArray *array = [NSMutableArray new];
    for (int i=0; i<20; i++) {
        DRListing *obj = [DRListing new];
        obj.featured = i*i%2;
        obj.claimed = i%2;
        obj.value = i*3%10;
        [array addObject:obj];
    }

    NSLog(@"Test Data: %@",array);
    return array;
}

@interface DRListing : NSObject

@property (nonatomic) BOOL featured;
@property (nonatomic) BOOL claimed;
@property (nonatomic) int value;
@end