I have an array of dictionaries as below.
NSMutableArray *newArray
({
{
A = John;
B = THAILAND;
},
{
A = Jack;
B = US;
}
{
A = Lee;
B = BRAZIL;
}
{
A = Brandon;
B = UK;
}
{
A = Jill;
B = JAPAN;
}
{
A = Johnny;
B = UK;
}
{
A = Amar;
B = AUSTRALIA;
}
})
I want to reorder the above array on the basis of another array (Key B of newArray) which is
NSArray *sortArray = [[NSArray alloc] initWithObjects"US",@"UK",nil];
That means my final array should contain US values first followed by UK followed by the rest. In short, how can I set my own order for an attribute/key in an Array.
How can I do that?
Expected Result :
NSMutableArray *finalArray
({
{
A = Jack;
B = US;
},
{
A = Brandon;
B = UK;
}
{
A = Johnny;
B = UK;
}
{
A = John;
B = THAILAND;
}
{
A = Lee;
B = BRAZIL;
}
{
A = Jill;
B = JAPAN;
}
{
A = Amar;
B = AUSTRALIA;
}
})
I have tried the below code which gives me ascending order of the key B.
[newArray sortUsingDescriptors:[NSArray arrayWithObjects: [[NSSortDescriptor alloc] initWithKey:@"B" ascending:YES], nil]];