github.com/emate/nomad@v0.8.2-wo-binpacking/ui/app/mixins/sortable.js (about)

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