Sort array of EmberJS objects by property
Ember MutableArray
has a sortBy
method:
As described here you can now sort your ArrayController.
The way you do it is provide extra properties on your ArrayController (pasted from link above):
songs = [ {trackNumber: 4, title: 'Ob-La-Di, Ob-La-Da'}, {trackNumber: 2, title: 'Back in the U.S.S.R.'}, {trackNumber: 3, title: 'Glass Onion'},];songsController = Ember.ArrayController.create({ content: songs, sortProperties: ['trackNumber'], sortAscending: true});songsController.get('firstObject'); // {trackNumber: 2, title: 'Back in the U.S.S.R.'}songsController.addObject({trackNumber: 1, title: 'Dear Prudence'});songsController.get('firstObject'); // {trackNumber: 1, title: 'Dear Prudence'}
EDIT: The below solution seems to only apply to numeric values. However this link will give advice on how to handle alpha-numeric, dates, etc: http://www.javascriptkit.com/javatutors/arraysort2.shtml
Nevermind, I figured it out. You can do it with Javascripts built in sort method:
//To sort ASCvar sorted = content.sort(function(a,b) { return a.get('propertyYouWantToSortBy') - b.get('propertyYouWantToSortBy');});//To sort DESCvar sorted = content.sort(function(a,b) { return b.get('propertyYouWantToSortBy') - a.get('propertyYouWantToSortBy');});