I am confused in one of the sorting & merging problem. Let me describe my problem as follows.
Step 1. I have an array. I am just writing the console log of an array to understand the structure of it.
(
{
id_ = 1,
name_ = "some xyz name",
date_ = "2012/01/24 05:25:30 AM"
},
{
id_ = 2,
name_ = "some ABC name",
date_ = "2012/01/25 06:25:30 AM"
},
{
id_ = 1,
name_ = "some PQR name",
date_ = "2012/01/22 04:30:30 AM"
},
)
Step 2. So, To sort above array, I have placed following code.
NSArray *myArray = // someHow I get those values listed above.
NSSortDescriptor *sortD1=[[NSSortDescriptor alloc] initWithKey:@"date_" ascending:YES];
myArray = [myArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortD1]];
Step 3. Now I have complete set of sorted array.
Step 4. I have similar kind of another array.
(
{
obj_id_ = 11,
obj_date_ = "2012/01/24 05:25:30 AM"
},
{
obj_id_ = 21,
obj_date_ = "2012/01/23 05:25:30 AM"
},
{
obj_id_ = 17,
obj_date_ = "2012/01/21 05:25:30 AM"
},
)
Step 5. So, To sort above array, I have placed following code.
NSArray *myArray2 = // someHow I get those values listed above.
NSSortDescriptor *sortD2=[[NSSortDescriptor alloc] initWithKey:@"obj_date_" ascending:YES];
myArray2 = [myArray2 sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortD2]];
Step 6. Now I have to merge both arrays but It should be sorted based on the date.
(
{
obj_id_ = 17,
obj_date_ = "2012/01/21 05:25:30 AM"
},
{
id_ = 1,
name_ = "some PQR name",
date_ = "2012/01/22 04:30:30 AM"
},
{
obj_id_ = 21,
obj_date_ = "2012/01/23 05:25:30 AM"
},
{
id_ = 1,
name_ = "some xyz name",
date_ = "2012/01/24 05:25:30 AM"
},
{
obj_id_ = 11,
obj_date_ = "2012/01/24 05:25:30 AM"
},
{
id_ = 2,
name_ = "some ABC name",
date_ = "2012/01/25 06:25:30 AM"
}
)
Expected output, I want to have is as above. Just that I don’t know the logic to merge two different arrays having different key-values. I am trying to build a logic for this but It returns an empty array & its wrong. So, I am not posting it here.
Note : If this question is duplicate, before down-voting it, inform me through comments. I will remove it & will be thankful for needful links & support. 🙂
First I would define a function that returns a date depending on the object that it is passed:
Then I would sort using blocks: