Monday, July 30, 2012

Best Way To Acess Array Containing Dictionary

While development many times it happens that we get data in dictionary trap in a array as below

Sample Data:

        ({
        0 = 31555800;
        1 = "0.3959247911412359168";
    },
        {
        0 = 63091800;
        1 = "0.4162550244325347328";
    },
        {
        0 = 94714200;
        1 = "0.5971193253467844608";
    },
        {
        0 = 126250200;
        1 = "1.5545692780580073472";
    })



let suppose we have more then 100 of data then the problem we face when we have to find:
 1. largest value on any key. (on key "1" is 1.5545692780580073472)
 2. Smallest value on any key. (on key "1" is 0.3959247911412359168)
 3. fetch all the values at any key to one array.
 4. fetch all the unique value at any key to one array.
 5. short the array according to any key value.


To Find max value at key "1" :
float max = [[dataArray valueForKeyPath:@"@max.1"] floatValue];

To Find min value at key "1" :
 float min = [[dataArray valueForKeyPath:@"@min.1"] floatValue];

To Fetch all the values at key "1":
 NSArray *allOne=[dataArray  valueForKeyPath:@"1"];

To find unique value at key "1":
NSArray *uniqueData=[dataArray  valueForKeyPath:@"@distinctUnionOfObjects.1"];


Shorting array according to key "1" values:

NSSortDescriptor *newDiscriptor = [[NSSortDescriptor alloc] initWithKey:@"1"  ascending:YES];
     
NSArray *descriptors = [NSArray arrayWithObjects:newDiscriptor, nil];        

NSArray *sortedArray = [dataArray sortedArrayUsingDescriptors:descriptors];

--------------------------------------------------------------------------------------------------------
I will post more if i find any other thing regarding array and dictionary :)

1 comment: