Complex array sorting question

  • OK, I've RTFM'd and don't see much documentation out there on
    sorting NSMutableArrays, so I thought I'd ask...

    I'd like to create a new data type that includes an NSString, an
    integer, and access methods for both the NSString and integer.
    Then I'd like to create objects of this type and store them into
    an NSMutableArray. (I've done this many times before.) Finally,
    I'd like to sort the NSMutableArray from time to time so the
    array is sorted in descending order based on the integer value
    within the object.

    I see NSMutableArray has two ways of sorting arrays, but both
    depend on external functions and there is no documentation on
    what I need to do to get this to work. I have no idea where to
    start... And I'd like to avoid bubble sorting if I can.

    So how do I go about defining and writing this function? Or are
    there some frameworks out there that do something like this
    already? Thanks...

    Nick Zitzmann
    ICQ: 22305512

    Check out my software page: http://homepage.mac.com/nickzman/
  • On Sunday, June 23, 2002, at 10:50 AM, Nick Zitzmann wrote:

    > OK, I've RTFM'd and don't see much documentation out there on sorting
    > NSMutableArrays, so I thought I'd ask...
    >
    > I'd like to create a new data type that includes an NSString, an
    > integer, and access methods for both the NSString and integer. Then I'd
    > like to create objects of this type and store them into an
    > NSMutableArray. (I've done this many times before.) Finally, I'd like
    > to sort the NSMutableArray from time to time so the array is sorted in
    > descending order based on the integer value within the object.
    >
    > I see NSMutableArray has two ways of sorting arrays, but both depend on
    > external functions and there is no documentation on what I need to do
    > to get this to work. I have no idea where to start... And I'd like to
    > avoid bubble sorting if I can.
    >
    > So how do I go about defining and writing this function? Or are there
    > some frameworks out there that do something like this already? Thanks...

    In the documentation for NSMutableArray, we have:

    - (void)sortUsingSelector:(SEL)comparator

    Sorts the receiver's elements in ascending order, as determined by the
    comparison method specified by the selector comparator. The comparator
    message is sent to each object in the array, and has as its single
    argument another object in the array. The comparator method is used to
    compare two elements at a time and should return NSOrderedAscending if
    the receiver is smaller than the argument, NSOrderedDescending if the
    receiver is larger than the argument, and NSOrderedSame if they are
    equal.

    See Also:  - sortUsingFunction:context:, - sortedArrayUsingSelector:
    (NSArray)

    So, if your class looks like this:

    @interface Foo : NSObject {
      NSString *name;
      int value;
    }

    - (NSString *)name;
    - (int)value;
    @end

    then you should add a comparison method to Foo, similar to this:

    @implementation Foo (SortingByValueDescending)

    - (NSComparisonResult) compareByValueDescending:(Foo *)other {
         if ([self value] == [other value]) {
             return NSOrderedSame;
         } else if ([self value] < [other value]) {
             return NSOrderedDescending;
         } else {
             return NSOrderedAscending;
         }
    }

    @end

    and then you can use this as follows,

    NSMutableArray *foos;

    foos = [foos sortUsingSelector:@selector(compareByValueDescending:)];

    > Nick Zitzmann

    // Christian Brunschen
previous month june 2002 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
MindNode
MindNode offered a free license !