stable sorting
hello,
there is a sorting task in the available version. If you want to sort one column, the previous order of other columns will be confused.
This is based on the sort-methode of the javascript-array-type. This sort-algorithm is not stable. I have taken an implementation of the Merge-Sort-Algorithm and replaced the sort-methode in the grid-object.
If anyone needs this:
obj.sort = function(index, direction)
{
// Direction validieren
if (direction && direction != "ascending" )
direction = "descending";
else
direction = "ascending";
// Die Rows in ihrer augenblicklichen Sortierung holen
var rows = this.getRowProperty("values");
// Die Werte der entsprechenden Spalte zum Vergleichen holen
var a = {};
for (var i=0; i<rows.length; i++)
a[rows[i]] = this.getDataProperty("value", rows[i], index);
// creating copy for workspace
var workSpace = new Array(rows.length);
// creating bounds
var lowerBound = 0;
var upperBound = rows.length - 1;
// sort
obj.mergeSort(workSpace, lowerBound, upperBound, rows, a, direction);
// setze Werte neu
this.setRowProperty("values", rows);
this.setSortProperty("index", index);
this.setSortProperty("direction", direction);
};
// --------------------------------------------------------------
// die MergeSort-Methode auf dem Object
obj.mergeSort = function(workSpace, lowerBound, upperBound, arrayToSort, arrayToCompare, direction)
{
if (lowerBound == upperBound) // if range is 1,
{
return; // no use sorting
}
else
{
// find midpoint
var mid = Math.floor((lowerBound + upperBound) / 2);
// sort low half
this.mergeSort(workSpace, lowerBound, mid, arrayToSort, arrayToCompare, direction);
// sort high half
this.mergeSort(workSpace, mid + 1, upperBound, arrayToSort, arrayToCompare, direction);
// merge them
this.merge(workSpace, lowerBound, mid + 1, upperBound, arrayToSort, arrayToCompare, direction);
}
}
// --------------------------------------------------------------
// die Merge-Methode für das Merge-Sort
obj.merge = function(workSpace, lowPtr, highPtr, upperBound, theArray, arrayToCompare, direction)
{
var j = 0; // workspace index
var lowerBound = lowPtr;
var mid = highPtr - 1;
var n = upperBound - lowerBound + 1; // # of items
while (lowPtr <= mid && highPtr <= upperBound)
{
if (direction == "ascending")
{
if (arrayToCompare[theArray[lowPtr]].toUpperCase() <= arrayToCompare[theArray[highPtr]].toUpperCase())
workSpace[j++] = theArray[lowPtr++];
else
workSpace[j++] = theArray[highPtr++];
}
else
{
if (arrayToCompare[theArray[lowPtr]].toUpperCase() >= arrayToCompare[theArray[highPtr]].toUpperCase())
workSpace[j++] = theArray[lowPtr++];
else
workSpace[j++] = theArray[highPtr++];
}
}
while (lowPtr <= mid)
workSpace[j++] = theArray[lowPtr++];
while (highPtr <= upperBound)
workSpace[j++] = theArray[highPtr++];
for (j = 0; j < n; j++)
theArray[lowerBound + j] = workSpace[j];
}
there is a sorting task in the available version. If you want to sort one column, the previous order of other columns will be confused.
This is based on the sort-methode of the javascript-array-type. This sort-algorithm is not stable. I have taken an implementation of the Merge-Sort-Algorithm and replaced the sort-methode in the grid-object.
If anyone needs this:
obj.sort = function(index, direction)
{
// Direction validieren
if (direction && direction != "ascending" )
direction = "descending";
else
direction = "ascending";
// Die Rows in ihrer augenblicklichen Sortierung holen
var rows = this.getRowProperty("values");
// Die Werte der entsprechenden Spalte zum Vergleichen holen
var a = {};
for (var i=0; i<rows.length; i++)
a[rows[i]] = this.getDataProperty("value", rows[i], index);
// creating copy for workspace
var workSpace = new Array(rows.length);
// creating bounds
var lowerBound = 0;
var upperBound = rows.length - 1;
// sort
obj.mergeSort(workSpace, lowerBound, upperBound, rows, a, direction);
// setze Werte neu
this.setRowProperty("values", rows);
this.setSortProperty("index", index);
this.setSortProperty("direction", direction);
};
// --------------------------------------------------------------
// die MergeSort-Methode auf dem Object
obj.mergeSort = function(workSpace, lowerBound, upperBound, arrayToSort, arrayToCompare, direction)
{
if (lowerBound == upperBound) // if range is 1,
{
return; // no use sorting
}
else
{
// find midpoint
var mid = Math.floor((lowerBound + upperBound) / 2);
// sort low half
this.mergeSort(workSpace, lowerBound, mid, arrayToSort, arrayToCompare, direction);
// sort high half
this.mergeSort(workSpace, mid + 1, upperBound, arrayToSort, arrayToCompare, direction);
// merge them
this.merge(workSpace, lowerBound, mid + 1, upperBound, arrayToSort, arrayToCompare, direction);
}
}
// --------------------------------------------------------------
// die Merge-Methode für das Merge-Sort
obj.merge = function(workSpace, lowPtr, highPtr, upperBound, theArray, arrayToCompare, direction)
{
var j = 0; // workspace index
var lowerBound = lowPtr;
var mid = highPtr - 1;
var n = upperBound - lowerBound + 1; // # of items
while (lowPtr <= mid && highPtr <= upperBound)
{
if (direction == "ascending")
{
if (arrayToCompare[theArray[lowPtr]].toUpperCase() <= arrayToCompare[theArray[highPtr]].toUpperCase())
workSpace[j++] = theArray[lowPtr++];
else
workSpace[j++] = theArray[highPtr++];
}
else
{
if (arrayToCompare[theArray[lowPtr]].toUpperCase() >= arrayToCompare[theArray[highPtr]].toUpperCase())
workSpace[j++] = theArray[lowPtr++];
else
workSpace[j++] = theArray[highPtr++];
}
}
while (lowPtr <= mid)
workSpace[j++] = theArray[lowPtr++];
while (highPtr <= upperBound)
workSpace[j++] = theArray[highPtr++];
for (j = 0; j < n; j++)
theArray[lowerBound + j] = workSpace[j];
}
Steve
March 3,