• 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: sorting two arrays
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sorting two arrays


  • Subject: Re: sorting two arrays
  • From: Steven Noyes <email@hidden>
  • Date: Tue, 29 Jul 2008 07:42:13 -0500

You might consider changing how you store data just a little bit. Instead of having two arrays, a single array with arrays as objects. For example, you currently have:

Array 1              Array 2
---------              -----------
|           |             |             |
---------              -----------
---------              -----------
|           |             |             |
---------              -----------
---------              -----------
|           |             |             |
---------              -----------
---------              -----------
|           |             |             |
---------              -----------

And you want the order of the two to remain the same. I would propose what you really want is:

Container array
----------------------------------------
| |
| --------- ----------- |
| | | ---------| | | objectAtIndex:0 is an array of two items and so on.
| --------- ----------- |
| --------- ----------- |
| | | ---------| | |
| --------- ----------- |
| --------- ----------- |
| | | ---------| | |
| --------- ----------- |
| --------- ----------- |
| | | ---------| | |
| --------- ----------- |
----------------------------------------


And then you sort (using sortUsingSelector:) the Container Array. Your selector would be within a category of NSMutalbeArray and compare what you want to compare for the sort.

To get to objects in Array, you would use [[containerArray objectAtindex:index] objectAtIndex:0] VS [Array 1 objectAtIndex:index].

You could also model the items as a new class (perhaps a bit cleaner and easier to maintain) but this might not be what you want.

Steven

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 (email@hidden)

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:
This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
This email sent to email@hidden


References: 
 >sorting two arrays (From: Jeff LaMarche <email@hidden>)

  • Prev by Date: [ANNOUNCE] CocoaHeads Frankfurt
  • Next by Date: Re: Storing values in dictionary with their address as the key
  • Previous by thread: Re: sorting two arrays
  • Next by thread: [Newbie] Communication between two Views?
  • Index(es):
    • Date
    • Thread