I have a Ember.ArrayController that has an unsorted content.
I want to know if its possible to sort the content of an ArrayController without using a new property.
I could of course create a new property:
App.MyArrayController = Em.ArrayController.extend({
mySortMethod: function(obj1, obj2) {
// some code here
},
updateSortedContent: function() {
var content = this.get('content');
if (content) {
var sortedContent = content.copy();
sortedContent.sort(this.mySortMethod);
this.set('sortedContent', sortedContent);
}
}.observes('content')
});
But I hope there is a better way that does not duplicates the content.
UPDATE
The latest version of Ember actually has sorting built in.
ArrayControllernow includes theEmber.SortableMixinwhich will engage if you specifysortProperties(Array) and optionallysortAscending(Boolean).Note: with the new SortableMixin, you still need to refer to
arrangedContentto get the sorted version. The model itself will be left untouched. (Thanks to Jonathan Tran)ORIGINAL ANSWER
The correct way to do this is to use the
arrangedContentproperty of ArrayProxy. This property is designed to be overridden to provide a sorted or filtered version of the content array.