3

I'm currently trying to teach myself Objective-C and was playing around with an exercise where I needed to sort an array.

I managed to complete it using the following code:

NSSortDescriptor * newSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:TRUE];
NSArray *sortDescriptors = [NSArray arrayWithObject:newSortDescriptor];
[self.theBookStore sortUsingDescriptors:sortDescriptors];

My question is about what is actually happening here. I don't really understand exactly what I've done.

Line 1: I understand here I've created a new object that has a descriptor. This has two parameters, the column I want to sort on and that it is ascending.

Line 2: This is the line I'm confused about. Why do I need an array of sort descriptors? When I read this code I suspect it creates an array with only one row is that correct?

Line 3: I understand that this is calling the sortUsingDescriptors method but again, my confusion is why this function expects an array.

I've read the documentation but I'm really looking for a simple explanation.

Any help is much appreciated

Chris Sanderson
  • 105
  • 1
  • 1
  • 5

3 Answers3

5

Line 1: I understand here I've created a new object that has a descriptor. This has two parameters, the column I want to sort on and that it is ascending.

Really, you've created an object that is a descriptor. It describes how to sort the array.

Line 2: This is the line I'm confused about. Why do I need an array of sort descriptors? When I read this code I suspect it creates an array with only one row is that correct?

Right -- you've created an array that contains a single object. You could create an array that has ten or fifteen or eighty-seven sort descriptors, if you really wanted to sort on that many fields. More often, you use one, two, maybe three. So, if you're sorting a list of people, you might add sort descriptors that specify last name and first name. That way, people that have the same last name will be arranged within that group according to their first name.

Line 3: I understand that this is calling the sortUsingDescriptors method but again, my confusion is why this function expects an array.

Again, it's so that you can have primary, secondary, tertiary (etc.) sort keys. You could have a separate method that takes a single sort descriptor instead of an array for those times when you want to sort on only one key. NSArray doesn't provide that, but you can always add it in a category if you want:

@category NSArray (SingleSortDescriptor)

- (NSArray*)sortUsingDescriptor:(NSSortDescriptor*)descriptor;

@end

@implementation NSArray (SingleSortDescriptor)

- (NSArray*)sortUsingDescriptor:(NSSortDescriptor*)descriptor
{
    return [self sortUsingDescriptors:[NSArray arrayWithObject:descriptor]];
}

@end
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • @Caleb : Welcome..Just one question I visited your profile. How do make it appear like that way? Looks nice.. – rohan-patel Apr 13 '12 at 15:13
  • @anonymous I'm not sure what you're referring to unless maybe it's the "flair" in the About Me section? Go to your profile, click on the "flair" link near the top ("edit prefs flair apps..."), and copy the HTML for the "combined Stack Exchange flair". Paste that into the "About Me" section of your profile. – Caleb Apr 13 '12 at 15:42
2
  • Line 1: .. yes your right. You are creating a custom object called NSSortDescriptor. That object defines a attribute to sort after. You did enter "title". So the objects in your array-to-sort will be sorted after that property (yourObject.title "kind-of").

  • Line 2: Because the sorting method (sortUsingDescriptors) always needs a array, you need to create a NSArray with only one object. Okay, ... looks kind of stupid. But makes absolute sense. Lets say you would like to sort after two criteria (lets say "title", then "city").

  • Line 3: Yes heres must be a array because of sorting after more then one criteria.

And always keep the memory clean: On line 1 you did allocate/init a NSSortDescriptor. So clean up after using it (if you are not using ARC). So add a line:

[newSortDescriptor release];
Community
  • 1
  • 1
Jonas Schnelli
  • 9,965
  • 3
  • 48
  • 60
  • We've had ARC for about a year now -- maybe it's time to start assuming people are using it rather than assuming that they're not. – Caleb Apr 13 '12 at 14:56
  • @JonasSchnelli And with it, my iPhone 3G... :-( – Caleb Apr 13 '12 at 15:43
1

Multiple sort descriptors would be used to resolve what happens if there are multiple matches. I's a priority order. A second descriptor would tell it what to do if it found two titles the same.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57