I want to sort objects with different descriptors keys).
The problem:
I have two arrays with populated with objects of different classes. and The two classes do not share a common attribute to use as a sort descriptor.
I want to sort the arrays alphabetically, and return only one array containing all the objects.
Array1 = with objects [A Class]
sorted by "name"
Array2 = with objects [B Class]
sorted by "title"
FinalArray => sort (again) alphabetically with all object's of Array's (1,2)
You can do it like this:
Create NSArray *result with the size of the combined arrays
Concatenate the two arrays into the result array by copying the elements of the first array followed by the elements of the second array into the result.
Use sortedArrayUsingComparator: with a comparator that "understands" both types.
Here is a skeletal implementation:
NSArray *sortedArray = [result sortedArrayUsingComparator: ^(id obj1, id obj2) {
NSString *key1, *key2;
if ([obj1 isKindOfClass:['class has a title' class]]) {
key1 = [obj1 title];
} else { // It's the kind of objects that has a name
key1 = [obj1 name];
}
if ([obj2 isKindOfClass:['class has a title' class]]) {
key2 = [obj2 title];
} else { // It's the kind of objects that has a name
key2 = [obj2 name];
}
// Do the comparison
return [key1 caseInsensitiveCompare:key2];
}];
One possible solution: add all array objects to an NSMutableDictionary, where the key is "name" or "title" as appropriate, then pull all of the dictionary keys, sort them, and retrieve by sorted key the objects into a new array:
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
for (a in array1) {
[dict setObject:a forKey:a.name]; // assumes name is property
}
for (b in array2) {
[dict setObject:b forKey:b.title]; // assumes title is property
}
NSArray* sortedKeys = [[dict allKeys] sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
NSArray* objects = [dict objectsForKeys:sortedKeys notFoundMarker:[NSNull null]];
Related
I have the following categoryNames array.
And now, I have categoryTempElements and it has cName property. I need to know how to order categoryTempElements with a order of categoryNames.
UPDATE: I have added sortOrder property to Category object and tried the following but order does not change.
for (Category* a in categoryTempElements) {
int index = (int)[categoryNames indexOfObject:a.cName];
a.sortOrder = index;
}
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"sortOrder" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray = [categoryTempElements sortedArrayUsingDescriptors:sortDescriptors];
Another way could be without using sortedArrayUsingComparator, using two for-loops. Declare a new Mutable array called sortedCategoryElements and compare the categoryNames in categoryTempElements, If matches add it to a new array sortedCategoryElements:
NSMutableArray *sortedCategoryElements = [NSMutableArray new];
for (NSString *name in categoryNames) {
for (Category *category in categoryTempElements) {
if (name == category.cName) {
[sortedCategoryElements addObject:category];
break;
}
}
}
I tried with your set of data, it worked for me.
Hope it helps!
You need first convert your categoryNames array into dictionary with NSString key and NSNumber int value, the value will be the order in the array
//this is example code, this will be your first array (reference value array)
NSArray * array = #[#"prueba",#"prueba2",#"prueba3"];
//first you need convert this array in NSDictionary
NSMutableDictionary * arrayDict = [NSMutableDictionary dictionary];
int counter = 0;
for (NSString * value in array) {
if(arrayDict[value] == nil)
{
arrayDict[value] = [NSNumber numberWithInt:counter];
}
counter++;
}
After that then you can get the value and order with sortedArrayUsingComparator method, something like this
//this is an example of your second array categoryTempElements
NSArray * arrayOfObjs = #[[testObject testObjectWithName:#"prueba3"],[testObject testObjectWithName:#"prueba"],[testObject testObjectWithName:#"prueba2"]];
NSArray * sorted = [arrayOfObjs sortedArrayUsingComparator:^NSComparisonResult(testObject * _Nonnull obj1, testObject * _Nonnull obj2) {
if([((NSNumber*)arrayDict[obj1.cName]) intValue] < [((NSNumber*)arrayDict[obj2.cName]) intValue]){
return NSOrderedAscending;
}
if([((NSNumber*)arrayDict[obj1.cName]) intValue] > [((NSNumber*)arrayDict[obj2.cName]) intValue]){
return NSOrderedDescending;
}
return NSOrderedSame;
}];
for (testObject * obj in sorted) {
NSLog(#"%#",obj.cName);
}
And voila in sorted you will have your array of object sorted by your first array NSString order
Hope this helps
Create a dictionary with cName as the key and the Category object as the value. Then iterate over the categoryNames array, and build another array by using each item in categoryNames as the key. The resulting array should be sorted in the same order as categoryNames.
NSArray has a method sortedArrayUsingComparator: which sorts an array using the ordering implemented by the block you pass. This block, of type NSComparator, is passed references to two elements of your array and you must return the order of those two elements.
And now, I have categoryTempElements and it has cName property.
So your block will be passed two categoryTempElements, you need to access the cName property of each, and compare the resulting two values...
I need to know how to order categoryTempElements with a order of categoryNames
by the position, i.e. the index, of those values in your categoryNames array. The method indexOfObject: provides that index for you.
So put that together and your problem is solved.
HTH
I have an array having objects with different properties. I want to create an array of sets which contain objects with same value of a single property of the object.
Suppose this is an array of object which has property a and b
1: {a:10, b:5}, 2: {a:2,b:5}, 3: {a:20,b:5}, 4: {a:5,b:5}, 5: {a:4,b:20}, 6: {a:51,b:20}
I want to create another array of NSSet of objects with distinct values of property b
so the result would be the following Array of 2 NSSet
1: {a:10, b:5}, {a:2,b:5}, {a:20,b:5}, {a:5,b:5}
2: {a:4,b:20}, {a:51,b:20}
How can this be done?
I'd do this by first creating a dictionary of sets where the keys of the dictionary are the unique values of "b".
Note: This is untested code. There could be typos here.
NSArray *objectArray = ... // The array of "SomeObject" with the "a" and "b" values;
NSMutableDictionary *data = [NSMutableDictionary dictionary];
for (SomeObject *object in objectArray) {
id b = object.b;
NSMutableSet *bSet = data[b];
if (!bSet) {
bSet = [NSMutableSet set];
data[b] = bSet;
}
[bSet addObject:object];
}
NSArray *setArray = [data allValues];
setArray will contain your array of sets.
This codes also assumes you have a sane isEqual: and hash implementation on your SomeObject class.
This is how you can do this:
NSArray *data = #[#{#"a":#10, #"b":#5}, #{#"a":#2,#"b":#5}, #{#"a":#4,#"b":#20}, #{#"a":#51,#"b":#20}];
NSSet *bSet = [NSSet setWithArray: [data valueForKey: #"b"]];
NSMutableArray *filteredArray = [[NSMutableArray alloc] initWithCapacity: bSet.count];
for (NSNumber *bValue in bSet) {
NSPredicate *anArrayFilterPredicate = [NSPredicate predicateWithBlock:^BOOL(NSDictionary *aDictionaryData, NSDictionary *bindings) {
if ([aDictionaryData[#"b"] isEqual:bValue]) {
return YES;
}
return NO;
}];
NSArray *uniqueBValueArray = [data filteredArrayUsingPredicate:anArrayFilterPredicate];
[filteredArray addObject:uniqueBValueArray];
}
NSLog(#"filteredArray = %#", filteredArray);
Using Key-Value coding collection operators, we can get the array of distinct values for an object. Then you could easily compute the results you want.
In your case this could be done like this.
NSArray *arrayOfDistinctObjects = [array valueForKeyPath:#"#distinctUnionOfObjects.b"];
NSMutableArray *newSetArray = [NSMutableArray new];
for (id value in arrayOfDistinctObjects) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.b == %#", value];
NSArray *filterArray = [array filteredArrayUsingPredicate:predicate];
NSSet *newSet = [NSSet setWithArray:filterArray];
[newSetArray addObject:newSet];
}
where array is the array of objects on which you wanna operate.
arrayOfDistinctObjects gives you the array of distinct values for b.
i have 2 nsarrays
1 with nsdictionary's another with nsnumbers
NSArray *arr1 = #[#{#"id":#1},#{#"id":#2},#{#"id":#3},#{#"id":#4}];
NSArray *arr2 = #[#3,#1,#4,#2];
and i want to sort my arr1 through their id following the order of arr2
is this possible?
The problem with using sortedArrayUsingComparator: is you start dealing with O(n^2) lookup times. For each sort comparison in the first array, you have to do a lookup in the second array.
Your best bet is to take advantage of a hash table to reduce that to O(n) average complexity.
Your first step is to create a dictionary using id as a key. The result would look something like #{#1: #{#"id":#"1"}, ...}. Then you just have to construct an array by looping through arr3 and grabbing the values.
NSArray *arr1 = #[#{#"id":#1},#{#"id":#2},#{#"id":#3},#{#"id":#4}];
NSArray *arr2 = #[#3,#1,#4,#2];
NSMutableDictionary *map = [NSMutableDictionary dictionary];
for (NSDictionary *item in arr1) {
map[item[#"id"]] = item;
}
NSMutableArray *arr3 = [NSMutableArray array];
for (id key in arr2) {
[arr3 addObject:map[key]];
}
This solution of course assumes parity between the two arrays. If arr2 has an element not in arr1 it will crash when trying to add nil to arr3. If arr1 has a value not in arr2 it will be excluded from arr3. These are risks you will have to address based on your requirements.
Here is how you can do it by using a custom comparator:
NSArray* sorted= [arr1 sortedArrayUsingComparator: ^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
return [arr2 indexOfObject:obj1[#"id"]] - [arr2 indexOfObject:[obj2[#"id"]];
}];
I exploited the fact that NSComparisonResult has +1 to represent an ascending order, -1 for descending and 0 to represent the same order.
- (NSArray*) sortedArray
{
NSArray *arr1 = #[#{#"id":#1},#{#"id":#2},#{#"id":#3},#{#"id":#4}];
NSArray *arr2 = #[#3,#1,#4,#2];
NSMutableArray *mutableArray = [NSMutableArray new];
for (NSNumber *number in arr2)
{
for (NSDictionary* dictionary in arr1)
{
NSNumber *number2 = dictionary[#"id"];
if ([number isEqual:number2])
{
[mutableArray addObject:dictionary];
break;
}
}
}
return mutableArray;
}
I have an NSMutableArray of many NSDictionaries that contain keys like "Title". In some cases there are duplicates of dictionaries with the same "Title" but differences in the other keys.
How can I remove the dictionaries that have the same "Title" key and leave only one in the array?
Thanks
Sort the array using NSSortDescriptor on the key path 'title'. Next, loop over the array and build a new array:
NSString *lastTitle = nil;
NSMutableArray *result = [NSMutableArray array];
for (NSDictionary *d in array) {
NSString *testTitle = [d objectForKey:#"title"];
if (![testTitle isEqualToString:lastTitle]) {
[result addObject:d];
lastTitle = testTitle;
}
}
Now result contains your filtered list.
It's important to sort the array first for this algorithm to work.
I am trying to copy the objects content of a NSDictionary to a NSMutableArray, and I am using the following code :
// Use when fetching binary data
NSData *responseData = [request responseData];
// View the data returned - should be ready for parsing.
resultsDictionary = [responseData objectFromJSONData];
NSLog(#"ResultsDictionary:%#", resultsDictionary);
self.OnlineObjects = [[[NSMutableArray alloc] init] autorelease];
for (NSDictionary * dataDict in resultsDictionary) {
[OnlineObjects insertObject:dataDict atIndex:0];
}
NSLog(#"OnlineObjects:%#", OnlineObjects);
This is working as i am getting all objects from the Dictionary, but the objects order have been revers, first object is now last ...
How can tell the insertObject to add the object at the last index ?
Thanks
You can use the addObject: method instead.
To get rid of the hash order problem get allKeys, sort the array and then use the elements as keys to get the objects in proper order.
Verbose example (for integer keys):
NSArray *indices = [[resultsDictionary allKeys] sortedArrayUsingComparator:^(id obj1, id obj2) {
if ( [obj1 intValue] > [obj2 intValue] ) {
return (NSComparisonResult)NSOrderedDescending;
}
if ( [obj1 intValue] < [obj2 intValue] ) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
for (int i = 0; i < [indices count]; i++) {
NSDictionary *obj = [resultsDictionary objectForKey:[indices objectAtIndex:i]];
[OnlineObjects addObject:obj];
}
The order of the elements in a NSDictionary is undefined, you don't know in which order they will be retrieved from the dictionary. The only way to do have the array sorted is to sort it once all the values from the dictionary are transferred to the array.
Two things you should know:
NSDictionary is a key-value container, which does not guarantee the order of the objects. You have no way to ensure that the order of inserting will be mantained when reading by using this data structure. Check other strategies if order is important for you, but do not rely on NSDictionary for this.
You have a couple of methods to extract the info of the keys and data: allKeys and allValues. Use them instead of creating your own.