• 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
NSMutableArray, TableView, Bindings and Sorting
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSMutableArray, TableView, Bindings and Sorting


  • Subject: NSMutableArray, TableView, Bindings and Sorting
  • From: <email@hidden>
  • Date: Sun, 25 Jul 2004 20:36:54 -0400

Hi,

I'm still playing around with bindings and tables. Everything is coming
together pretty well. Now I'm looking into sorting. I know the table view, that
is bound to an NSArrayController (whose contantArray is bound to my own
NSMutableArray)) will sort when you click on the table heading. I also realized
that doing that does not sort the actual array.

In trying to figure out what to do if I wanted to sort the actual array, I did
some tests..

I created a button in the window to actually sort the array. I display the
contents before and after the sort, but it isn't being sorted! This is the action
method of the button:

- (IBAction)showArray:(id)sender
{

Book *o;
int i;

NSLog(@"Before");
NSEnumerator *e = [booksList objectEnumerator];
while (o = [e nextObject])

{
NSLog(@"Book = %@, Author = %@", [o title], [o author]);
}

[booksList sortedArrayUsingFunction:myCompare context:nil];

NSLog(@"After");
e = [booksList objectEnumerator];
while (o = [e nextObject])
{
NSLog(@"Book = %@, Author = %@", [o title], [o author]);
}
}

And the compare function is :


int myCompare(id left, id right, void *ctxt) {
NSLog(@"%@ %@ %d", [(Book *)left title], [(Book *)right title],
[(NSString*)[(Book *)left title] compare:[(Book *)right title]]);
return [(NSString*)[(Book *)left title] compare:[(Book *)right title]];
}

When I run, this is what is output:

2004-07-25 20:23:47.183 Bibliotecha[5209] Before
2004-07-25 20:23:47.183 Bibliotecha[5209] Book = dog, Author = (null)
2004-07-25 20:23:47.183 Bibliotecha[5209] Book = cat, Author = (null)
2004-07-25 20:23:47.183 Bibliotecha[5209] Book = bird, Author = (null)
2004-07-25 20:23:47.183 Bibliotecha[5209] dog cat 1
2004-07-25 20:23:47.184 Bibliotecha[5209] dog bird 1
2004-07-25 20:23:47.184 Bibliotecha[5209] cat bird 1
2004-07-25 20:23:47.184 Bibliotecha[5209] After
2004-07-25 20:23:47.184 Bibliotecha[5209] Book = dog, Author = (null)
2004-07-25 20:23:47.184 Bibliotecha[5209] Book = cat, Author = (null)
2004-07-25 20:23:47.184 Bibliotecha[5209] Book = bird, Author = (null)

Ummmm.. Why isn't the array being sorted?

I haven't got to the point of making sure the table view is updated after the
sort. And on that point, is there a way of, when clicking on the table header,
to make sure the array itself it sorted?

Oh, the NSMutableArray is made up of objects of a custom class with two
members, both NSString's (the sample is from Mike Beam's Cocoa Controllers
article on MacDevCenter.)

Thanks!

jt
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


  • Follow-Ups:
    • Re: NSMutableArray, TableView, Bindings and Sorting
      • From: mmalcolm crawford <email@hidden>
  • Prev by Date: Re: Obj-C header-file becoming C header-file, & NSImage problem
  • Next by Date: WebView Scroll to end
  • Previous by thread: textview continuous spell checking
  • Next by thread: Re: NSMutableArray, TableView, Bindings and Sorting
  • Index(es):
    • Date
    • Thread