github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/ui/app/mixins/sortable.js (about)

     1  import Ember from 'ember';
     2  
     3  const { Mixin, computed } = Ember;
     4  
     5  /**
     6    Sortable mixin
     7  
     8    Simple sorting behavior for a list of objects.
     9  
    10    Properties to override:
    11      - sortProperty: the property to sort by
    12      - sortDescending: when true, the list is reversed
    13      - listToSort: the list of objects to sort
    14  
    15    Properties provided:
    16      - listSorted: a copy of listToSort that has been sorted
    17  */
    18  export default Mixin.create({
    19    // Override in mixin consumer
    20    sortProperty: null,
    21    sortDescending: true,
    22    listToSort: computed(() => []),
    23  
    24    listSorted: computed('listToSort.[]', 'sortProperty', 'sortDescending', function() {
    25      const sorted = this.get('listToSort')
    26        .compact()
    27        .sortBy(this.get('sortProperty'));
    28      if (this.get('sortDescending')) {
    29        return sorted.reverse();
    30      }
    31      return sorted;
    32    }),
    33  });