I utilized a table view, bound to an NSArrayController, who’s data is being stored in the Shared User Defaults in my application.
The table only has a single column.
I’m wondering what the easiest way would be to sort the values alphabetically, ascending order? Can this be done with binding? Or should I add some type of sort code to the “apply preferences” function I have in my application?
Most of the examples I’m finding for this are all OBJ C.
You can use NSSortDescriptor Class method sortDescriptorWithKey:ascending:selector: and call the array method on it. something like this:
From the Docs
Creates an NSSortDescriptor with the specified ordering and comparison selector.
(id)sortDescriptorWithKey:(NSString *)key ascending:(BOOL)ascending selector:(SEL)selector
set anArray to current application's NSArray's arrayWithArray:arrayList
set sortDesc to current application's NSSortDescriptor's sortDescriptorWithKey:sortField ascending:sortAscending selector:"caseInsensitiveCompare:"
Hope this helps.
Not sure I quite understand. All I have in my script right now is a property for the array controller:
property genreArrayController : missing value
And I’m grabbing the list of values using something like:
How do I get to the underlying array being handled by the controller?
Sorry, I am not following. BTW, if you have Shane Stanley’s book, he has projects on the arrayController class in which he illustrates sorting tables using an array Controller.
I do, which is where I got this originally from. I’ll have to dig into it more this weekend.
Are yo jable to post the code you found in Objective-C. Translation cam be straight forward at times.
So you create sort descriptors and tell your array controller to use them. Something like:
set theDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:theKey ascending:ascendingOrder
You may also need to call rearrangeObjects.
I actually decided to just enable the column header, and I set it to be case insensitive sortable via that (I followed Shane’s book). This is actually better as it allows the user to choose if they want it sorted or not.
My only issue now is that it’s not writing the sorted version out to the shared user defaults. Is there a “continuously updates” for the array controller?
I have the auto-arrange setting on.
Well I have a user configurable list of items (they are music genres).
The user adds a genre, another one, then they click the “Name” header for the column to sort the values and click “Apply”.
This genre lists later is used in a pull down menu, so they probably want it sorted (which it does nicely).
Then when you close and restart the application, everything is back to unsorted… not so good.
Sort them when you reopen.
I got this to work the way I wanted. For the NSArrayController, you also need to bind the sort descriptors to the shared user defaults controller.
It now remembers the sorting preferences between restarts!