I have an NSArray that contains some NSString objects. For instance:
NSArray *objects = @[@"Stin",@"Foo",@"Ray",@"Space"];
Now I need to sort this array based on the next row order.
NSArray *sortOrder = @[@"John",@"Foo",@"Space",@"Star",@"Ray",@"Stin"];
So the answer should be
NSArray *sorted = @[@"Foo",@"Space",@"Ray",@"Stin"];
How can i achieve this?
ANSWER: Based on the accepted dasblinkenlight answer, I did the following and it worked to charm.
NSMutableArray *objects = @[@"Star",@"Stin",@"Foo",@"Ray",@"Space",@"John"]; NSArray *sortOrder = @[@"John",@"Foo",@"Space",@"Star",@"Ray",@"Stin"]; [objects sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { int index1 = [sortOrder indexOfObject:obj1]; int index2 = [sortOrder indexOfObject:obj2]; if (index1 > index2) return NSOrderedDescending; if (index1 < index2) return NSOrderedAscending; return NSOrderedSame; }];