# Default Sort Property for ManyArray

**URL:** <https://discuss.emberjs.com/t/default-sort-property-for-manyarray/4942>\
**Category:** Ember Data\
**Created:** [April 18, 2014, 12:01pm UTC](https://discuss.emberjs.com/t/default-sort-property-for-manyarray/4942 "2014-04-18T12:01:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhruv\_parmar](https://avatars.discourse-cdn.com/v4/letter/d/ecd19e/32.png) [@dhruv\_parmar](https://discuss.emberjs.com/u/dhruv_parmar)\
**Post date:** [April 18, 2014, 12:01pm UTC](https://discuss.emberjs.com/t/default-sort-property-for-manyarray/4942/1 "2014-04-18T12:01:34Z")

</div>

I have a proposal to include a functionality where we can provide the default sortProperty for a DS.ManyArray. In Most cases we end up using an extra computed property to achieve this. Here is an example use case

```auto
App.Commentable = DS.Model.extend
  comments:DS.hasMany('comment',{async:true})
  sortedComments:(->
    @get("comments").toArray().sort((a,b)->
      b.get('createdAt')-a.get('createdAt')
    )
  ).property("comments.@each.isFulfilled")

```

The above was my use case where I had to sort the comments on the created\_at with the most latest being the first. It would have been good if I could do something likes this.

```auto
App.Commentable = DS.Model.extend
  comments:DS.hasMany('comment',{async:true,sort:"createdAt"})

```

---

<div class="post-metadata">

**Author:** ![alexspeller](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/alexspeller/32/14812_2.png) [@alexspeller](https://discuss.emberjs.com/u/alexspeller)\
**Post date:** [April 18, 2014, 4:23pm UTC](https://discuss.emberjs.com/t/default-sort-property-for-manyarray/4942/2 "2014-04-18T16:23:10Z")

</div>

It seems that an ArrayController would be the best way to handle this:

```auto
App.CommentsController = Em.ArrayController.extend({
  sortProperties: ['createdAt']
});

```

---

<div class="post-metadata">

**Author:** ![alexspeller](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/alexspeller/32/14812_2.png) [@alexspeller](https://discuss.emberjs.com/u/alexspeller)\
**Post date:** [April 18, 2014, 4:24pm UTC](https://discuss.emberjs.com/t/default-sort-property-for-manyarray/4942/3 "2014-04-18T16:24:03Z")

</div>

Or if you don’t want to use an ArrayController then you can use `Em.computed.sort` [http://emberjs.com/api/#method\_computed\_sort](http://emberjs.com/api/#method_computed_sort)

---

<div class="post-metadata">

**Author:** ![dhruv\_parmar](https://avatars.discourse-cdn.com/v4/letter/d/ecd19e/32.png) [@dhruv\_parmar](https://discuss.emberjs.com/u/dhruv_parmar)\
**Post date:** [April 18, 2014, 4:54pm UTC](https://discuss.emberjs.com/t/default-sort-property-for-manyarray/4942/4 "2014-04-18T16:54:36Z")

</div>

Thanks I guess the computed\_sort method fits the bill perfectly.
