The most logical is to make a class that has - as properties - the two pieces of data being sorted, then put these in an NSArray (or NSMutableArray as appropriate to your situation), and then sort that instead. Not only will it sort both pieces of data together, but it's a lot easier to extend if you need more data sorted along with the key array.

So if array1 is an array of 'int', and array2 is an array of 'NSString *', then you would make a class like:

@interface sortableData : NSObject

{
int sortKey;
NSString *data;
}

// useful methods here...
@end

then - in your .m file - implement a sort method that takes an 'NSArray *' pointing to the array filled with references to sortableData objects. Now, when you sort it, all the other data (the NSStrings) come along for the ride for free. The other way to do it is - when you do a swap() on one array's elements i and j, you swap the elements i and j of (all) the other array(s) related to the key value array (the one you're directly sorting on.) Finally, put the data into a database with an SQL front-end, and do the sort that way.
  That said, I hope you get it working. :)

On Jul 28, 2008, at 10:08 AM, Jeff LaMarche wrote:

Hey all...

I've got two arrays that are related, so that object 0 in one array corresponds to object 0 in the other array. I'd like to implement a sort based on the values in one array, but to reorder both array so that the order of the two arrays stays in sync and that object x in one array continues to correspond to the same thing as object x in the other array after the sort.

I've got a general idea of how to implement the sort, but since I know that there are a few places in the system where array pairs are used like this, I wanted to see if there was any out-of-the-box way of handling this scenario - I'd hate to reinvent the wheel if it's not necessary. Does anyone know if there is any easy way to do this?

TIA,
Jeff

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/wsquires% 40satx.rr.com

This email sent to [EMAIL PROTECTED]

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to