• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Sort Array
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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>)

  • Prev by Date: missing textDidEndEditing: NSTextView delegate messages
  • Next by Date: Re: Close button on Single Window Utilities
  • Previous by thread: Re: Sort Array
  • Next by thread: Re: Sort Array
  • Index(es):
    • Date
    • Thread