Class Ember.SortableMixin

Ember.SortableMixin provides a standard interface for array proxies to specify a sort order and maintain this sorting when objects are added, removed, or updated without changing the implicit order of their underlying content array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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'}

If you add or remove the properties to sort by or change the sort direction the content sort order will be automatically updated.

1
2
3
4
5
songsController.set('sortProperties', ['title']);
songsController.get('firstObject'); // {trackNumber: 2, title: 'Back in the U.S.S.R.'}

songsController.toggleProperty('sortAscending');
songsController.get('firstObject'); // {trackNumber: 4, title: 'Ob-La-Di, Ob-La-Da'}

SortableMixin works by sorting the arrangedContent array, which is the array that arrayProxy displays. Due to the fact that the underlying 'content' array is not changed, that array will not display the sorted list:

1
2
songsController.get('content').get('firstObject'); // Returns the unsorted original content
songsController.get('firstObject'); // Returns the sorted content.

Although the sorted content can also be accessed through the arrangedContent property, it is preferable to use the proxied class and not the arrangedContent array directly.