Re: Sorted arrays
Re: Sorted arrays
- Subject: Re: Sorted arrays
- From: Andy Lee <email@hidden>
- Date: Wed, 8 Jun 2005 11:15:26 -0400
On Jun 8, 2005, at 10:37 AM, Andy Bettis wrote:
I want to store a list of objects in a NSMutableArray, which will
be kept sorted. How can I ensure that new items are inserted with
the correct index? Assuming that there's no way of assigning a
comparator to an array (which I can't find any reference to)
I found something called NSSortDescriptor that you can use with
NSArrayController. I haven't used the Controller classes, so I can't
advise you beyond pointing out this exists. If you want to stick
with a plain and simple NSMutableArray, then your assumption is
correct -- there is no built-in way to assign a comparator that
causes it to automatically remain sorted.
is there a way of finding the index of where an object would be
inserted according to the isEqual() message results?
I assume you don't really mean -isEqual:, but more on that in a bit.
Of course you can write your own logic to do this. The quickest to
code would be a loop that iterates through the array until it finds
the right insertion point, using whatever ordering criterion you
want. That might be perfectly fine for your purposes. It it's too
slow, you could use binary search, which requires more careful coding
to handle boundary cases (though you should be able to find working
code or pseudo-code on the 'net). If that's still too slow, you
probably need a different data structure.
Looking ahead, I want to be able to change the sorting criteria for
my data, probably via a separate array which could also be a subset
of the complete data set. This will make isEqual() a poor choice,
as I don't want to implement the viewing criteria inside the model
objects.
-isEqual: is probably not what you want for sorting purposes, since
it doesn't tell you which object is "greater than" or "less than."
What you really want depends on what kind of objects you are
comparing. For NSString and NSDate and some other classes, there is -
compare:. For NSString, there is -caseInsensitiveCompare:. If you
have AppKiDo, you can search for "compare" and find other related
methods.
If you have your own, more complex comparison criteria (like if your
objects aren't simple strings), you can wrap them in a method or
function and call that whenever you need to compare two objects.
Any advice? I'd rather not subclass NSMutableArray in my first
Cocoa project!
You don't have to subclass NSMutableArray to add your own custom
insertion logic. You could put the logic somewhere else and call
that wrapper method instead of an NSMutableArray method. Or you can
add a category to NSMutableArray:
@implementation NSMutableArray (MyCustomInserts)
// This method assumes the receiver is sorted using comparison ABC.
// On exit, it will still be sorted.
- (void)myInsertObject:(MyClass *)obj
{
// ...do the insertion...
}
// This method assumes the receiver is sorted using comparison XYZ.
// On exit, it will still be sorted.
- (void)myOtherInsertObject:(MyClass *)obj
{
// ...do the insertion...
}
@end
Then you *would* be able to say
[myMutArray myInsertObject:myObject];
--Andy
_______________________________________________
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