I have an object:
var Data = [{
item_id:1,
name:'John',
date:1262293200000,
votes:1
}, {
item_id:2,
name:'Nick',
date:1313784000000,
votes:2
},{
item_id:3,
name:'Paul',
date:1299186000000,
votes:-3
}]
I want to sort it by item_id, name, date and votes. Asc and desc. To do this I use this function:
function dynamicSort(property) {
return function (a,b) {
return (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0; }}
Array.prototype.sortBy = function(property) { return this.sort(dynamicSort(property)) }
Array.prototype.reverseBy = function(property) { return this.reverse(dynamicSort(property)) }
It's sorts and reverses nice, but only frow second calling. For example:
videoDataList.reverseBy("user_votes")
result will be wrong, but if I do sortBy and then again reverseBy it will be correct sorting.
Also if i call reverseBy and then sortBy sorting of sortBy will be correct.
Is it possible to fix?