Depending on the result from ABPersonGetSortOrdering (), I want to sort UILocalizedIndexCollation by first or last name.
I'm having problems with the @selector switch used for the collationStringSelector parameter.
It would be very simple to write it verbose:
NSArray * sortedSubarray;
if (ABPersonGetSortOrdering () == 0) {
sortedSubarray = [collation sortedArrayFromArray: [sections objectAtIndex: section] collationStringSelector: @selector (fname)];
} else {
sortedSubarray = [collation sortedArrayFromArray: [sections objectAtIndex: section] collationStringSelector: @selector (lname)];
}
I tried something like this with no luck:
SEL sorter = ABPersonGetSortOrdering () == 0? NSSelectorFromString (@ "fname"): NSSelectorFromString (@ "lname");
sortedSubarray = [collation sortedArrayFromArray: [sections objectAtIndex: section] collationStringSelector: @selector (sorter)];
I also tried other ideas and nothing works.
Is there a better way to pass a selector name dynamically?
source share