• 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: Selecting max in NSArray
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Selecting max in NSArray


  • Subject: Re: Selecting max in NSArray
  • From: Florijan Stamenkovic <email@hidden>
  • Date: Fri, 11 Feb 2005 13:59:10 +0100

Of course you might want to get the object without sorting the array, both for performance or other reasons.

public Item itemWithMaxPrice(EOEditingContext ec)
{
NSArray objects = EOUtilities.objectsForEntityNamed(ec, "Item");
if ( (int count = objects.count()) == 0 )
return null;
Item returnItem = (Item)objects.objectAtIndex(0);
for(int i = 0 ; i < count ; i++)
{
if ( ((Item)objects.objectAtIndex(i)).price().intValue() > returnItem.price().intValue() )
returnItem = (Item)objects.objectAtIndex(i);
}
return returnItem;
}




On Feb 11, 2005, at 05:34, Robert Walker wrote:

Drew,

You could write a static method in you business class something like this:

I would do something like this:

public static Item itemWithMaxPrice(EOEditingContext ec) {
NSArray objects = EOUtilities.objectsForEntityNamed(ec, "Item");
NSArray sortedObjects = EOSortOrdering.sortedArrayUsingKeyOrderArray(objects, priceOrderings());
return (MyEO)sortedObjects.lastObject();
}


public static NSArray priceOrderings() {
EOSortOrdering priceOrdering = EOSortOrdering.sortOrderingWithKey("price", EOSortOrdering.CompareAscending);
return new NSArray(priceOrdering);
}


On Feb 10, 2005, at 10:58 PM, Drew Thoeni wrote:

I have an NSArray with several objects in it. I want to select the object that has the maximum price. However, using valueForKey("@max.item.price") returns, of course, the price for the item instead of the item. Is there a way to do this without having to refetch?

Drew

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


--
Robert Walker
www.robertwalker1.com

"There are 10 kinds of people in the world. Those who think binary and those who don't."
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Selecting max in NSArray (From: Drew Thoeni <email@hidden>)
 >Re: Selecting max in NSArray (From: Robert Walker <email@hidden>)

  • Prev by Date: Re: Selecting max in NSArray
  • Next by Date: Re: Bizarre Behavior
  • Previous by thread: Re: Selecting max in NSArray
  • Next by thread: RE: Selecting max in NSArray
  • Index(es):
    • Date
    • Thread