Sorting NSArray of dictionaries by value of a key in the dictionaries
Solution 1
I think this will do it:
brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@"brand" ascending:YES];
sortDescriptors = [NSArray arrayWithObject:brandDescriptor];
sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors];
I pulled the code from Sort Descriptor Programming Topics. Also, Key-Value Coding comes into play, in that sortedArrayUsingDescriptors:
will send a valueForKey:
to each element in myArray
, and then use standard comparators to sort the returned values.
Solution 2
We Got The Solution By Using The Method Follows
[self.jsonData sortUsingDescriptors: [NSArray arrayWithObjects: [NSSortDescriptor sortDescriptorWithKey:"fullname" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:"id" ascending:NO], nil]];
Where:-
jsonData
- MutableArray Which holds the Parsed JSON Data.
fullname
- the data we want to sort.
id
- An unique data which comes with the inner dictionary.
Solution 3
In switf:
var descriptor: NSSortDescriptor = NSSortDescriptor(key: "brand", ascending: true)
var sortedResults: NSArray = results.sortedArrayUsingDescriptors([descriptor])
Solution 4
Use following code for sort using the "brand" key from the dictionary..
NSSortDescriptor * brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@"brand" ascending:YES];
NSArray * sortDescriptors = [NSArray arrayWithObject:brandDescriptor];
NSArray * sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors];
NSLog(@"sortedArray %@",sortedArray);
Use following code, If you to sorting according two keys from the dictionary; Like, "brand" key and productTitle key from the dictionary:-
NSSortDescriptor * brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@"brand" ascending:YES];
NSSortDescriptor * productTitleDescriptor = [[NSSortDescriptor alloc] initWithKey:@"productTitle" ascending:YES];
NSArray * sortDescriptors = [NSArray arrayWithObjects:brandDescriptor, productTitleDescriptor, nil];
NSArray * sortedArray = [feedData sortedArrayUsingDescriptors:sortDescriptors];
NSLog(@"sortedArray %@",sortedArray);
Solution 5
arrSorted = [arrBrand sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
if ([[obj1 valueForKey:@"iUserId"] integerValue] > [[obj2 valueForKey:@"iUserId"] integerValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
if ([[obj1 valueForKey:@"iUserId"] integerValue] < [[obj2 valueForKey:@"iUserId"] integerValue]) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
manuelBetancurt
Computers, BJJ, Open Source, Psichotronics "Ars longa, vita brevis, occasio praeceps, experimentum periculosum, iudicium difficile", "Life is short, [the] craft long, opportunity fleeting, experiment treacherous, judgment difficult." Hippocrates "Hay cosas tan grandes y complejas que solo el hombre indicado o un loco se atreven a afrontar!!", "There are things so great and complex that only the right man or a nut case dare to face!!" Don Juan
Updated on May 19, 2020Comments
-
manuelBetancurt almost 4 years
I have an array populated by dictionaries, and I need to sort the array alphabetically by the values of one of the keys of the dictionaries.
This is my array:
tu dictus: ( { brand = Ryul; productTitle = Any; quantity = 1; subBrand = "Ryul INJ"; type = Product; }, { brand = Trol; productTitle = Different; quantity = 2; subBrand = ""; type = Brand; }, { brand = Dtor; productTitle = Any; quantity = 1; subBrand = ""; type = Product; }, { brand = Ryul; productTitle = Different; quantity = 2; subBrand = "Ryul CHES"; type = SubBrand; }, { brand = Anan; productTitle = Any; quantity = 1; subBrand = ""; type = Product; } )
Normally for sorting an array I will use
myArray = [uniqueProdsArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
But how do sort using the
brand
key of the dictionary? -
gnasher729 over 8 yearsWhile sortedArrayUsingComparator is the way to go, having up to four calls to "valueForKey" instead of two calls to "objectForKey" is bad. BTW. Since you expect dictionaries, use NSDictionary* as the type, not id.
-
Admin over 8 yearshow to use this in swift2 ?