PDA

View Full Version : JSONView memory sort



MarkT
9 Jun 2007, 11:04 PM
At the request of a couple of my beta testers, I altered the JSONView's sort function so it remembers the previous sort property and sorts records with the same sort value by the previous sort. A simple two dimensional sort option. I figured I would share it here since I've gotten so much from the Ext. community.


JSONView.sort = function(property, dir, sortType) {
var prevSortInfo = this.sortInfo;
this.sortInfo = Array.prototype.slice.call(arguments, 0);
if (this.jsonData) {
var p = property;
var dsc = dir && dir.toLowerCase() == "desc";
var f = function(o1, o2) {
var v1 = sortType ? sortType(o1[p]) : o1[p];
var v2 = sortType ? sortType(o2[p]) : o2[p];
if(v1 < v2){
return dsc ? +1 : -1;
} else if(v1 > v2){
return dsc ? -1 : +1;
} else {
if(prevSortInfo){
var p2 = prevSortInfo[0];
var dsc2 = prevSortInfo[1] && prevSortInfo[1].toLowerCase() == "desc";
var x1 = sortType ? sortType(o1[p2]) : o1[p2];
var x2 = sortType ? sortType(o2[p2]) : o2[p2];
if(x1 < x2){
return dsc ? +1 : -1;
} else if(x1 > x2){
return dsc ? -1 : +1;
} else {
return 0;
}
} else {
return 0;
}
}
};
this.jsonData.sort(f);
this.refresh();
if (this.jsonData != this.snapshot) {
this.snapshot.sort(f);
}
}
}

I also setup a default secondary sort on a different view by just changing the first line of the function to point to an array with the default sort info. :)