简体   繁体   English

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

[英]Remove array elements and add them at the same index iOS

I am sorting an array. 我正在排序一个数组。 There are three types of elements in the array. 数组中有三种类型的元素。 1. featured 2. organic and 3. claimed. 1.特色2.有机和3.宣称。

Among them, I want to sort only organic elements and keep the featured and claimed elements at their own index. 其中,我只想对有机元素进行排序,而将特征元素和声明的元素保留在自己的索引中。

Below is my code in which, I am extracting the claimed and featured indices in a dictionary as key being the index and value is the array element. 下面是我的代码,其中,在字典中提取声明的索引和特征索引,因为键是索引,而值是数组元素。

//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];

}

Again I am adding the claimed and featured back to their original indices after sorting as: 在排序为:之后,我再次将要求保护的特征添加到其原始索引中:

 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];
        }
    }
}

The code works good. 该代码运行良好。 Except there is claimed/(and)featured elements at the last index of the 'array'. 除非在“数组”的最后一个索引处有要求保护/(和)功能的元素。 Because the 'sortedArray' index remains less than the 'array.count' in this scenario. 因为在这种情况下'sortedArray'索引仍然小于'array.count'。 Thanks in advance. 提前致谢。

Update - 更新-

I receive response array of type: 我收到以下类型的响应数组:

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

and I am allowed to sort only organic elements within this array. 并且我只能在此数组内对有机元素进行排序。 Featured and claimed should not loose their original index position. 精选并声明不应失去其原始索引位置。

I would iterate through the array, extracting the organics to sort. 我将遍历数组,提取要排序的有机物。 Then sort your organic array. 然后排序您的有机阵列。 Then iterate through the original array taking either the element from the original array or an element from the sorted organics array as appropriate. 然后迭代原始数组,适当时采用原始数组中的元素或已排序有机物数组中的元素。

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];
    }
}

You could possibly make it a little more efficient if you reversed your sort order for the organics array since then you could say 如果您颠倒了有机物数组的排序顺序,则可能会使其更有效率,因为您可以说

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

But if your array isn't particularly large then the performance improvement will probably be negligible. 但是,如果您的阵列不是特别大,那么性能改进可能会忽略不计。

Maybe this is an alternative: 也许这是另一种选择:

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];

You can take the first 2 functions. 您可以使用前两个功能。 The others are what I used for testing. 其他是我用来测试的。

- (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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 删除iOS中的数组元素问题? - Remove array elements issue in iOS? 如何将静态元素添加到UICollectionViewController或使它们与它共享相同的屏幕 - how to add static elements to UICollectionViewController or make them share the same screen with it iOS swift 从另一个数组中移除一个数组的元素 - iOS swift remove elements of an array from another array iOS UItableView通过索引路径从数组中删除对象 - iOS UItableView remove object from array by index path 如何在CoreData iOS中的实体中的同一索引处添加两个项目 - How to add two items at the same index in an entity in CoreData iOS 如何在同一数组中添加具有相同结尾编号的元素? (斯威夫特 3) - How to add the elements with the same ending number in the same array? (Swift 3) 按索引移动数组中的元素 - Shift elements in array by index 5 分钟后定时器完成时如何删除单元格索引 ios swift 5,当调用 api 时不再重复相同索引的定时器 - how to remove cell index when timer gets complete after 5 min ios swift 5 , when called api not repeat timeragain of same index 在iOS中显示和删除相同的按钮 - show and remove same button in iOS 如何在 Swift iOS 中比较两个数组并从一个数组中删除匹配的元素 - How to compare two arrays and remove matching elements from one array in Swift iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM