canjs / can-list-sort

Live-bound list sorting
MIT License
1 stars 0 forks source link

Simple syntax for descending sort #2

Open justinbmeyer opened 8 years ago

justinbmeyer commented 8 years ago

From: https://github.com/canjs/canjs/issues/1643

It would be nice to have a syntax for supporting descending sort. Writing a manual comparator function is ok, but there could be some sugar that can could provide. Any thoughts on the below code?

var list = new can.List([{
  priority: 4,
  name: 'low'
}, {
  priority: 1,
  name: 'high'
}, {
  priority: 2,
  name: 'middle'
}, {
  priority: 3,
  name: 'mid'
}]);

// Option 1: Adding a predicate before the property.
list.attr('comparator', '-priority');

// Option 2: Using an options object.
list.attr('comparator', { property: 'priority', direction: 'desc'})

\cc: @robr24, @alexisabril