JSFiddle - React, Tailwind, and code Playground
HTML
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.0/moment.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>
<script src="http://backbonejs.org/backbone.js"></script>
<!-- <script src="http://backbonejs.org/backbone.js"></script> -->
<div id="log">
</div>
JavaScript
$(document).ready(function () {
ReportListItem = Backbone.Model.extend({
defaults: function () {
return {
"modificationDate": null
}
}
})
MyCollection = Backbone.Collection.extend({
sortField: "modificationDate",
descending: true,
model: ReportListItem,
comparator: function (a, b) {
var value1 = a.get(this.sortField)
var value2 = b.get(this.sortField)
// date needs to be parsed before usage
if (this.sortField === "modificationDate") {
value1 = moment(value1)
value2 = moment(value2)
if (this.descending) {
if(value1.isBefore(value2))
return -1
else if(value1.isAfter(value2))
return 1
else
return 0
}
if (value1.isAfter(value2))
return 1
else if (value1.isBefore(value2))
return -1
else
return 0
}
if (this.descending) {
return value1 > value2 ? -1
: value1 < value2 ? 1
: 0
}
return value1 > value2 ? 1
: value1 < value2 ? -1
: 0
}
})
var item1 = new ReportListItem({ modificationDate: new Date(1389174631000) } )
var item2 = new ReportListItem({ modificationDate: new Date(1389121649000) } )
var item3 = new ReportListItem({ modificationDate: new Date(1389118794000) } )
var item4 = new ReportListItem({ modificationDate: new Date(1388683574000) } )
var item5 = new ReportListItem({ modificationDate: new Date(1388499055000) } )
var collection = new MyCollection()
...