predicate with smallest x-coordinate?

  • I have a Fish class with two ivars declared like this

    @interface Fish : NSObject
    {
        NSString *name;
        NSRect frame;
    }
    ....

    I create a NSMutableArray which contains a number of Fish objects. When I want to filter out all Fish objects whose name begins with letter B i use

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name beginswith 'B'"];

    That works. But now I want to filter out the Fish that has the smallest x-coordinate (that is the smallest frame.origin.x). How should I write my predicate?

    Thanks Bob

    __________________________________________________
    Do You Yahoo!?
    Tired of spam?  Yahoo! Mail has the best spam protection around
    http://mail.yahoo.com
  • I doubt that a predicate well suited since it would have to know about
    all Fish in order to determine whether or not a given Fish had the
    smallest x-coordinate.  Why not sort the array and take the first
    element, or simply iterate over the list and take the smallest.  You
    could make the latter more visually pleasing by implementing a
    category on NSArray...

    @interface(NSArray)
    - (id) leastObjectUsingSelector:(SEL)compareSelector;
    @end

    dave

    On 8-Nov-07, at 8:17 AM, Bob Ueland wrote:

    > I have a Fish class with two ivars declared like this
    >
    > @interface Fish : NSObject
    > {
    > NSString *name;
    > NSRect frame;
    > }
    > ....
    >
    >
    > I create a NSMutableArray which contains a number of Fish objects.
    > When I want to filter out all Fish objects whose name begins with
    > letter B i use
    >
    > NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name
    > beginswith 'B'"];
    >
    >
    > That works. But now I want to filter out the Fish that has the
    > smallest x-coordinate (that is the smallest frame.origin.x). How
    > should I write my predicate?
    >
    > Thanks Bob
    >
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today