:: Forum >>

Sort then reverse ? Why ?


Perhaps there is no need of calling rows.reverse(); when sorting is hapenning already.

Following modification will make descending faster...

grid.js (rel 1.0) - line #740

obj.sort = function(index, direction) {

var model = this.getModel("row");
if (model.sort) {
return model.sort(index, direction);
}

var a = {};
var rows = this.getRowProperty("values");

if (direction && direction != "ascending" ) {
direction = "descending";
}
else {
direction = "ascending";
}

if (this.getSortProperty("index") != index) {
var idir = 1;
if (direction == "descending") {
idir = -1;
}
for (var i=0; i<rows.length; i++) {
a[rows[i]] = this.getDataProperty("value", rows[i], index);
}
rows.sort(function(x,y){return a[x] > a[y] ? idir : (a[x] == a[y] ? 0 : -1 * idir)});
// if (direction == "descending") {
// rows.reverse();
// }
}
else if (this.getSortProperty("direction") != direction) {
rows.reverse();
}


this.setRowProperty("values", rows);
this.setSortProperty("index", index);
this.setSortProperty("direction", direction);
};
Sudhaker Raj
Thursday, July 15, 2004
Yes, probably this makes sense.
Alex (ActiveWidgets)
Friday, July 16, 2004
I did a patch for sorting by 2 or 3 keys ;-) It is simple but useful method for most of practical usages. U may like to have it included in your build or as a separate patch.

Active.Controls.Grid.patch = function(){
var obj = this.prototype;

/****************************************************************

Sorts the rows with the data in the given column.

@param index1 (Number) Column index to sort on.
@param direction1 (String) Sort direction ([a|ascending] or [d|descending]).
@param index2 (Number) Column index to sort on.
@param direction2 (String) Sort direction ([a|ascending] or [d|descending]).
@param index3 (Number) Column index to sort on.
@param direction3 (String) Sort direction ([a|ascending] or [d|descending]).

*****************************************************************/

obj.defineSortProperty("index1", -1);
obj.defineSortProperty("direction1", "none");
obj.defineSortProperty("index2", -1);
obj.defineSortProperty("direction2", "none");
obj.defineSortProperty("index3", -1);
obj.defineSortProperty("direction3", "none");

obj.tripleSort = function(index1, direction1, index2, direction2, index3, direction3) {
var model = this.getModel("row");
if (model.tripleSort) {
return model.tripleSort(index1, direction1, index2, direction2, index3, direction3);
}

var d1 = 1;
var d2 = 1;
var d3 = 1;

if (direction1 == "d" || direction1 == "descending") {
d1 = -1;
}
if (direction2 == "d" || direction2 == "descending") {
d2 = -1;
}
if (direction3 == "d" || direction3 == "descending") {
d3 = -1;
}

var a1 = {};
var a2 = {};
var a3 = {};
var rows = this.getRowProperty("values");
for (var i=0; i<rows.length; i++) {
if(index1 != null) {
a1[rows[i]] = this.getDataProperty("value", rows[i], index1);
}
if(index2 != null) {
a2[rows[i]] = this.getDataProperty("value", rows[i], index2);
}
if(index3 != null) {
a3[rows[i]] = this.getDataProperty("value", rows[i], index3);
}
}

var _tripleSort = function(x,y){
if(index1 != null && a1[x] != a1[y]) {
return a1[x] > a1[y] ? d1 : -1 * d1;
}
if(index2 != null && a2[x] != a2[y]) {
return a2[x] > a2[y] ? d2 : -1 * d2;
}
if(index3 != null && a3[x] != a3[y]) {
return a3[x] > a3[y] ? d3 : -1 * d3;
}
return 0;
};
rows.sort(_tripleSort);

this.setRowProperty("values", rows);
this.setSortProperty("index1", index1);
this.setSortProperty("direction1", direction1);
this.setSortProperty("index2", index2);
this.setSortProperty("direction2", direction2);
this.setSortProperty("index3", index3);
this.setSortProperty("direction3", direction3);
};

};

Active.Controls.Grid.patch();


Thanks.
Sudhaker Raj
Friday, July 16, 2004

This topic is archived.


Back to support forum

Forum search