views:

55

answers:

2

I'd prefer not to change the way anArray is designed because it is also used elsewhere as a dataSource for a UITableView. The odd number values are "user names", and the even number values are their matching "dates". (The "pairs" must remain together.)

How would I sort "by user name"?

How would I sort "by date"?

Should I be using sortUsingFunction or sortUsingSelector?

-(void) test
{
    NSMutableArray *anArray = [NSMutableArray arrayWithObjects:@"Zeke", @"01-Jan-2010", @"Bob", @"02-Jan-2010", @"Fred", @"03-Jan-2010", @"Susan", @"04-Jan-2010", @"Kim", @"05-Jan-2010", @"Debbie", @"06-Jan-2010", nil];
    [anArray sortUsingFunction:SortByDate context:(void *)context];
}

NSComparisonResult SortByDate(NSMutableArray *a1, NSMutableArray *a2, void *context) 
{
   // What goes here to keep the pair-values "together"?
}
+1  A: 

Why aren'e you using a NSDictionary? You can have user names as keys and the dates as corresponding values. Then if you want to sort on user names, just use [dictObj allkeys] to get an array containing just the keys. Sort them as you prefer and then when displaying display the sorted array of keys and fetch the corresponding value for the key and display along side. Similarly, you can get all the values using [dictObj allValues] sort them and display them along with keys.

Hetal Vora
Ugh. That involves using -[NSDictionary allKeysForObject:] which can't be very fast.
tc.
A: 

I'd prefer not to change the way anArray is designed because it is also used elsewhere as a dataSource for a UITableView

Does that mean cells alternate between displaying usernames and dates, or are you multiplying/dividing by 2?

Either way, you need to fix your model. The bit where you go "oops, sorting doesn't work" should be a big hint that you haven't chosen a good representation of your data. Adding a workaround will only lead to more tears later.

I'd suggest something like this:

  1. Create a class to represent your (username,date) tuple.
  2. Add -compareByDate: and -compareByUsername: methods.
  3. [array sortUsingSelector:@selector(compareByDate:)]

If you want to display usernames and dates in alternating cells, then it's still easy to do, but I'd simply use a taller cell and save myself some pain.

Note: The overhead of Objective-C method calls means that you can get a significant performance increase by using sortUsingComparator: and defining appropriate functions (you can stick the function between @implmentation and @end to let them access protected/private ivars) but it's not really worth doing this unless you've determined that sorting is a bottleneck.

tc.
Unfortunately, it's impossible to add more classes, more code, more complexity to this mess we inherited years ago. I can't have yet another conversation where I try to convince other depts in other cities to redesign their code... just so we can "sort 7 items and save 0.001 seconds of time" in a 'correct' manner. Is there truly NO way possible to "sort items in pairs, in the existing NSArray design"? (If I tell them "it's totally impossible"... I'm sure THAT will also create major problems, too.)
Patricia
Oh, *that* kind of code. I'm sorry ☹ I'd just wrap them in classes, sort them, and unwrap them again.
tc.
But *HOW* would I wrap them in classes, sort them, and unwrap them again?
Patricia