Re: Sort Array
Re: Sort Array
- Subject: Re: Sort Array
- From: email@hidden
- Date: Sat, 16 Nov 2002 14:38:19 +0100
On vendredi, novembre 15, 2002, at 06:11 PM, Lorenzo Puleo wrote:
Hi,
I have a short list with "Name" and "Surname" fields.
I want to sort this list by "Surname".
How should I build the NSArray?
How should I use the NSArray methods?
A possible solution (I'm not saying it's the best one):
Build a NSMutableArray of NSDictionary:
In the dictionary, you have the duos:
Key:"Name" Value: "William"
Key:"Surname" Value: "Smith"
To sort the array or dictionary by Surname, you need to implement a
Category for NSDictionary. I'm typing it without the documentation so
the code might be wrong, but the point is on the archicture
@interface NSDictionary (SortBySurname)
- ((NSComparisonResult)) sortBySurname:(NSDictionary *) adictionary;
@end
@implementation NSDictionary (SortBySurname)
- ((NSComparisonResult)) sortBySurname:(NSDictionary *) adictionary
{
return [[self objectForKey:@"Surname"] compare: [adictionary
objectForKey:@"Surname"]];
}
@end
And then to sort your Array, you call:
[myMutableArray sortUsingSelector:@selector(sortBySurname:)];
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: | |
| >Sort Array (From: Lorenzo Puleo <email@hidden>) |