• 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: Binary search on sorted NSArray in Cocoa?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Binary search on sorted NSArray in Cocoa?


  • Subject: RE: Binary search on sorted NSArray in Cocoa?
  • From: "Karan, Cem (Civ, ARL/CISD)" <email@hidden>
  • Date: Tue, 6 Jan 2009 11:42:49 -0500
  • Thread-topic: Binary search on sorted NSArray in Cocoa?

Adam R. Maxwell wrote on Tuesday, January 06, 2009 9:59 AM:

> On Jan 6, 2009, at 8:16 AM, Karan, Cem (Civ, ARL/CISD) wrote:
>
> >  My question is, does NSArray already have a binary search method
> > defined on it in a category somewhere?  If it does, I'd rather use
> > that.
>
> It's not a category, but take a look at CFArrayBSearchValues,
> which works just fine thanks to toll-free bridging.

That looks just like what I need!  I know I'm being pedantic, but just
to make sure, will the following code work?

	// mySortedArray is an NSArray of NSString instances that has
been sorted
	// via caseInsensitiveCompare:.  The strings within the array
are unique;
	// you'll never see the same string twice in the array.
myString is an
	// NSString instance that may or MAY NOT be within the array.

	CFRange arrayRange;
	arrayRange.location = 0;
	arrayRange.length = [mySortedArray count];

	CFIndex stringIndex;
	stringIndex = CFArrayBSearchValues(	mySortedArray,
							arrayRange,
							myString,
							CFStringCompare,

kCFCompareForcedOrdering);

	if ((stringIndex < 0) || (stringIndex >= [mySortedArray count]))
	{
		// Didn't find myString, do something intelligent
	}
	else if ([myString isEqualToString:[mySortedArray
objectAtIndex:stringIndex]] == NO)
	{
		// Still didn't find myString, do something intelligent
	}
	else
	{
		// Found myString's actual index, do the work I need to
do and move on.
	}

Thanks,
Cem Karan
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Binary search on sorted NSArray in Cocoa?
      • From: Ken Thomases <email@hidden>
References: 
 >Binary search on sorted NSArray in Cocoa? (From: "Karan, Cem (Civ, ARL/CISD)" <email@hidden>)
 >Re: Binary search on sorted NSArray in Cocoa? (From: "Adam R. Maxwell" <email@hidden>)

  • Prev by Date: Re: Synchronizing QTMovieLayers
  • Next by Date: Re: Determining width of a cell in an NSOutlineView
  • Previous by thread: Re: Binary search on sorted NSArray in Cocoa?
  • Next by thread: Re: Binary search on sorted NSArray in Cocoa?
  • Index(es):
    • Date
    • Thread