Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge & Sort objects in Array contained in Dictionary

I'm getting this kind of array of data from a server (it dynamically changes):

layers =     (
            {
        maximum = 4;
        minimum = 3;
        name = "layer 1";
    },
            {
        maximum = 19;
        minimum = 8;
        name = "layer 1";
    },
            {
        maximum = 1;
        minimum = 1;
        name = "layer 38";
    },
            {
        maximum = 4;
        minimum = 1;
        name = "layer 3";
    },
            {
        maximum = 24;
        minimum = 15;
        name = "layer 3";
    }
);

in this case, I have 5 objects in 1 array. every object has name, minimum value and maximum value.

The code should have a loop which stats from the minimum value to the maximum value. for example, if the minimum is 10 and maximum is 13, the values should be: 10,11,12,13.

I want to put together the objects with the same name, into 1 object. As I said, this data is dynamically changes. it can has any random data, but if there are equal objects, they will be placed one after each other, like here with "layer 1" objects and "layer 3" objects.

The final result I want is this:

{
    name = "layer 1";
    values =         (
        3,
        4,
        8,
        9,
        10,
        11,
        12,
        13,
        14,
        15,
        16,
        17,
        18,
        19
    );
},
    {
    name = "layer 38";
    values =         (
        1,
        1
    );
},
    {
    name = "layer 3";
    values =         (
        1,
        2,
        3,
        4,
        15,
        16,
        17,
        18,
        19,
        20,
        21,
        22,
        23,
        24
    );
}

The code I tried to run is:

 layers = [[NSMutableArray alloc]init];


         NSMutableArray *layer = [[NSMutableArray alloc]init];
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        [layers addObject:[mivne_shichva objectAtIndex:0]];  
                   for (int i=0; i<mivne_shichva.count; i++) {
         //   NSLog(@"i=%d",i)         
                if ([[[mivne_shichva objectAtIndex:i]valueForKey:@"name"]isEqualToString:[[layers objectAtIndex:i] valueForKey:@"name"]]) {


                    NSLog(@"EQUALS");
                   layer = [[NSMutableArray alloc]init];                          
                    int minimum = [[[mivne_shichva objectAtIndex:i]valueForKey:@"minimum"]intValue];
                    int maximum = [[[mivne_shichva objectAtIndex:i]valueForKey:@"maximum"]intValue];
                    for (int x = minimum; x<=maximum; x++) {

                        [layer addObject:[NSString stringWithFormat:@"%d",x]];
                        NSLog(@"%@",layer);

                 // dic = [[NSMutableDictionary alloc]init];
                    [dic setValue:[[mivne_shichva objectAtIndex:i]valueForKey:@"name"] forKey:@"name"];
                    [dic setValue:layer forKey:@"values"];
                    [layers addObject:dic];                              

                    }  
                }else {


                 layer = [[NSMutableArray alloc]init];  
                    int minimum = [[[mivne_shichva objectAtIndex:i]valueForKey:@"minimum"]intValue];
                    int maximum = [[[mivne_shichva objectAtIndex:i]valueForKey:@"maximum"]intValue];
                    for (int x = minimum; x<=maximum; x++) {

                       [layer addObject:[NSString stringWithFormat:@"%d",x]];
                        NSLog(@"%@",layer);

                    }

                  NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];  
                    [dic setValue:[[mivne_shichva objectAtIndex:i]valueForKey:@"name"] forKey:@"name"];
                        [dic setValue:layer forKey:@"values"];

                        [layers addObject:dic];  

                    NSLog(@"NOT EQUALS");
                }



        }

But I don't understand how to make it works...

THANKS IN ADVANCE FOR ANY CODE AND HELP!

like image 958
Yosi Dahan Avatar asked Mar 28 '26 08:03

Yosi Dahan


1 Answers

Your sortedArrayDict contains key as Layer#, and value as an array with sorted and merged value based on same layers.

Layer Class :

@interface Layer : NSObject

@property(strong) NSString *name;
@property(assign) NSInteger minimum;
@property(assign) NSInteger maximum;

@end

#import "AppDelegate.h"
#import "Layer.h"

@implementation AppDelegate

- (id)init
{
    self = [super init];
    if (self) {

        _finalDictionary=[NSMutableDictionary new];

        Layer *layer1=[Layer new];
        layer1.name=@"Layer 1";
        layer1.minimum=3;
        layer1.maximum=4;

        Layer *layer2=[Layer new];
        layer2.name=@"Layer 1";
        layer2.minimum=8;
        layer2.maximum=19;

        Layer *layer3=[Layer new];
        layer3.name=@"Layer 38";
        layer3.minimum=1;
        layer3.maximum=1;

        Layer *layer4=[Layer new];
        layer4.name=@"Layer 1";
        layer4.minimum=2;
        layer4.maximum=4;

        Layer *layer5=[Layer new];
        layer5.name=@"Layer 3";
        layer5.minimum=24;
        layer5.maximum=15;

        _initialArray=[[NSMutableArray alloc]initWithObjects:layer1,layer2,layer3,layer4,layer5, nil];

    }
    return self;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{

    for (Layer *layer in _initialArray) {
        if ([_finalDictionary objectForKey:layer.name]) {
            //append array
            NSMutableArray *array=[_finalDictionary objectForKey:layer.name];
            [array addObject:[NSNumber numberWithInteger:[layer minimum]]];
            [array addObject:[NSNumber numberWithInteger:[layer maximum]] ];
        }
        else{
            //add new key
            NSMutableArray *array=[NSMutableArray new];
            [array addObject:[NSNumber numberWithInteger:[layer minimum]]];
            [array addObject:[NSNumber numberWithInteger:[layer maximum]] ];
            [_finalDictionary setObject:array forKey:layer.name];
        }
    }

    //sorting
    for (int i=0; i<_finalDictionary.count; i++){
        NSString *dict=[[_finalDictionary allKeys] objectAtIndex:i];
        NSArray *arrayToSort=[_finalDictionary objectForKey:dict];
        NSArray *sortedArray=[arrayToSort sortedArrayUsingSelector:@selector(compare:)];
        [_finalDictionary setObject:sortedArray forKey:dict];
    }

    //printing
    for (NSDictionary *dict in _finalDictionary) {
        NSLog(@"Label: %@",dict);
        for (Layer *layer in [_finalDictionary objectForKey:dict]) {
            NSLog(@"Value: %@",layer);
        }
    }
}

Output:

2012-12-20 18:45:32.344 MergeArray[1357:403] Label: Layer 1
2012-12-20 18:45:32.346 MergeArray[1357:403] Value: 2
2012-12-20 18:45:32.347 MergeArray[1357:403] Value: 3
2012-12-20 18:45:32.347 MergeArray[1357:403] Value: 4
2012-12-20 18:45:32.348 MergeArray[1357:403] Value: 4
2012-12-20 18:45:32.349 MergeArray[1357:403] Value: 8
2012-12-20 18:45:32.349 MergeArray[1357:403] Value: 19
2012-12-20 18:45:32.350 MergeArray[1357:403] Label: Layer 38
2012-12-20 18:45:32.350 MergeArray[1357:403] Value: 1
2012-12-20 18:45:32.351 MergeArray[1357:403] Value: 1
2012-12-20 18:45:32.352 MergeArray[1357:403] Label: Layer 3
2012-12-20 18:45:32.360 MergeArray[1357:403] Value: 15
2012-12-20 18:45:32.362 MergeArray[1357:403] Value: 24
like image 177
Anoop Vaidya Avatar answered Mar 29 '26 21:03

Anoop Vaidya