I have an array appd.arrOfDictAppProd, in there is a a key Price which is a String value. But i want to sort this array by Price value. So I fetch Price key from appd.arrOfDictAppProd array and convert Price to String to Int, then make an NSMutableArray newarray without any key. I want to sort this using NSSortDescriptor this without use of any Key because in my newarray have no key. My code is here:
for (int i = 0; i<appd.arrOfDictAppProd.count; i++) {
NSString *price_Value = [[appd.arrOfDictAppProd objectAtIndex:indexPath.row] objectForKey:@"price"];
int price_IntValue = [price_Value intValue];
NSLog(@"int value of prices:%d",price_IntValue);
NSNumber *num = [NSNumber numberWithInteger:price_IntValue];
NSLog(@"number-%@",num);
[newarray addObject:num];
}
NSLog(@"price array:%@",newarray);
NSSortDescriptor *sortDescriptor;
sortDescriptor =[[NSSortDescriptor alloc] initWithKey:@"num"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray1;
sortedArray1 = [newarray sortedArrayUsingDescriptors:sortDescriptors];
When i run my program, in the newarray I have 14 values but these are same, that is 3.
Possibly I've missed the point but it sounds like you're massively overcomplicating things.
If you wanted to create
newarray
regardless — or just without your manual loop — then go with:The mechanism relied upon here is key-value coding. I've also built upon the specific way that
NSDictionary
andNSArray
implement key-value coding.-valueForKey:
and-valueForKeyPath:
are provided byNSObject
. Ignoring special cases and fallbacks, an object will respond to the former by returning the value of that property as an object — amongst other things it'll automatically convert built-in numeric types toNSNumber
s. The latter will traverse object hierarchies, as e.g.object.property1.property2
will requestobject
, then requestproperty1
fromobject
, then requestproperty2
fromproperty1
.You can therefore use
[stringObject valueForKey:@"intValue"]
to access theintValue
property onstringObject
and then haveNSObject
package it up into anNSNumber
.NSDictionary
has an implementation of key-value coding that will look into the dictionary for an appropriate value unless it's prefixed with an@
to mean that you want information about the dictionary, not from the dictionary. Because your key name is a string that doesn't begin with an@
,valueForKey:
therefore ends up callingobjectForKey:
.Therefore the sort descriptor with key
price.intValue
on an array of dictionaries will ask each dictionary for the value for keyprice
. The dictionary will decide to callobjectForKey:
. It'll get a string back. It'll callintValue
on that string and get anint
back. It'll then wrap that into anNSNumber
and compare the numbers for all dictionaries to decide the ordering.NSArray
implementsvalueForKey:
andvalueForKeyPath:
by calling the corresponding method on each item in the array in turn and then returning the array containing all those results. So you can use key-value coding as a way to map results to a certain extent.