UPDATE
The latest version of Ember has built-in sorting. ArrayController now includes Ember.SortableMixin , which will be enabled if you specify sortProperties (Array) and optionally sortAscending (Boolean).
Note: with the new SortableMixin, you still need to refer to arrangedContent to get a sorted version. The model itself will remain untouched. (Thanks to Jonathan Tran)
App.userController = Ember.ArrayController.create({ content: [], sortProperties: ['age'], sortAscending: false })
ORIGINAL RESPONSE
The proper way to do this is to use the arrangedContent property for ArrayProxy. This property is intended to be overridden to provide a sorted or filtered version of the content array.
App.userController = Ember.ArrayController.create({ content: [], sort: "desc", arrangedContent: Ember.computed("content", function() { var content, sortedContent; content = this.get("content"); if (content) { if (this.get("sort") === "desc") { this.set("sort", "asc"); sortedContent = content.sort(function(a, b) { return a.get("age") - b.get("age"); }); } else { this.set("sort", "desc"); sortedContent = content.sort(function(a, b) { return b.get("age") - a.get("age"); }); } return sortedContent; } else { return null; } }).cacheable() });
Ivan
source share