• 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: Sorting array of strings by string length
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sorting array of strings by string length


  • Subject: Re: Sorting array of strings by string length
  • From: Rosyna <email@hidden>
  • Date: Tue, 21 Feb 2006 07:09:41 -0700

Alternatively you can use sortedArrayUsingFunction:context: without risking the name space collisions that categories have at times.

And if you don't want to have to rewrite your category much, just call the first parameter of your function self. Like:

NSComparisonResult LengthCompare(id self, id other, void* context)
{
return [[NSNumber numberWithInt:[other length]] compare:[NSNumber numberWithInt:[self length]];



}

then you just call:

[array sortedArrayUsingFunction:LengthCompare context:NULL];

Ack, at 2/20/06, Scott Anguish said:

you'd need to write your own sort method to use with it, but it's pretty easy.

You'd add a category to the objects you want to sort.


On Feb 20, 2006, at 5:42 PM, Nik Youdale wrote:

Hi,

How would i go about sorting an array of strings in ascending/descending order of string lengths. Is there an easy way to do this, like using something like:

[array sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)], is there a different selector i could put in there that would sort the array by string length?

--


Sincerely, Rosyna Keller Technical Support/Holy Knight/Always needs a hug

Unsanity: Unsane Tools for Insanely Great People

It's either this, or imagining Phil Schiller in a thong.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Sorting array of strings by string length (From: Nik Youdale <email@hidden>)
 >Re: Sorting array of strings by string length (From: Scott Anguish <email@hidden>)

  • Prev by Date: NSManagedObjectContext won't save...
  • Next by Date: Re: COM programming in Mac
  • Previous by thread: Re: Sorting array of strings by string length
  • Next by thread: Custom object in core data.
  • Index(es):
    • Date
    • Thread