You'll need to define a custom sorting function, then pass it to an NSArray method that allows custom sorting. For example, using sortedArrayUsingFunction:context:
, you might write (assuming your types are NSString instances):
NSInteger customSort(id obj1, id obj2, void *context) {
NSString * type1 = [obj1 type];
NSString * type2 = [obj2 type];
NSArray * order = [NSArray arrayWithObjects:@"topic", @"users", @"questions", nil];
if([type1 isEqualToString:type2]) {
return NSOrderedSame; // same type
} else if([order indexOfObject:type1] < [order indexOfObject:type2]) {
return NSOrderedDescending; // the first type is preferred
} else {
return NSOrderedAscending; // the second type is preferred
}
}
// later...
NSArray * sortedArray = [myGenericArray sortedArrayUsingFunction:customSort
context:NULL];
If your types aren't NSStrings, then just adapt the function as needed - you could replace the strings in the order
array with your actual objects, or (if your types are part of an enumeration) do direct comparison and eliminate the order
array entirely.