Sorting items in an NSArray

Moderator
Posts: 508
Joined: 2002.09
Post: #1
Hi,

I have a little problem that's bugging me. In my app I use an NSArray to store objects of a certain class. I also have an NSTableView that reflects the contents of the NSArray.

My problem is that if I want to add drag and drop, so the user can sort the table the way he wants, I will have to sort the array by replacing and removing objects. Since removing items from an NSArray sends a release message (and thus a dealloc), can I sort the array without having to remove items?
The object needs to be alive after the sorting operation and having to make it again is not an option.

Thanks in advance.

"When you dream, there are no rules..."
Quote this message in a reply
Hog
Member
Posts: 151
Joined: 2002.09
Post: #2
an NSArray does not really seem suitable for that purpose. you might be better off using an NSMutableArray.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2006.10
Post: #3
Both NSArray and NSMutableArray have methods which will do the sorting for you.
NSArray (and hence NSMutableArray) has a sortedArrayUsingSelector: method which returns a new sorted array, allowing you to dispose of the old unsorted one. NSMutableArray also has sortUsingSelector: which will sort the array in place.

If for some reason you are determined to use your own sorting algorithm, you just have to send each object in the array a retain message before you take it out.
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #4
Assuming you are autoreleasing or releasing your objects once they are put into the array (which you should be doing), then yes, you have to retain them when sorting by hand. Then to do your sorting by hand:

Code:
myTempObj = [[MyNSMutableArray objectAtIndex:1] retain];
[MyNSMutableArray removeObjectAtIndex:1];
[MyNSMutableArray insertObject:myTempObj atIndex:0];
[myTempObj release];
//I've moved the second object to the first position in the NSMutableArray
;

note to belthaczar: please read the entire parent post before posting.

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2006.10
Post: #5
kelvin Wrote:note to belthaczar: please read the entire parent post before posting.

Sorry, I didn't catch the fact that he wanted to exchange them two at a time. In which case NSMutableArray also has a method for that purpose. [edit]well, never mind, perhaps that is not what he wants either.[/edit]
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #6
yeah retaining the object before actually sorting did the trick.

Thanks a bunch.

"When you dream, there are no rules..."
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Die-hard vertex-sorting function not accepting input values! mikey 6 3,825 Oct 31, 2009 03:36 AM
Last Post: mikey
  squares[] and sorting vertices into arrays... mikey 3 2,975 Sep 11, 2009 11:21 AM
Last Post: mikey
  NSArray Weirdness MikeC 4 3,836 Jan 5, 2008 06:41 PM
Last Post: MikeC
  Depth Sorting algorithm Leroy 1 4,509 Jul 2, 2007 01:47 AM
Last Post: aegidian
  Removing Vector Items In Loop? Nick 5 3,088 Oct 21, 2006 06:20 PM
Last Post: Nick