3.0.0

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];
}
Steve
March 3,
Hi Steve,
Can you please give me a example of MergeSort . I have a scenario where I lose the sorting on my first column in case I call sorting on some other column. Can I use MergeSort in this case ?? Please let me know your inputs

Regards,
April 25,
Hi Steve ,
I got an easy method in JavaScript . Have a look , the number of lines of codes are less and could be done on multiple columns.The sorting is of character and numeric sort.

function mysortfn(a,b) {

if (a[0]<b[0]) return -1;
if (a[0]>b[0]) return 1 ;
//Sorting the 1th Column after sorting 1st/5th column records
if (a[1]<b[1]) return a[1] - b[1];
if (a[1]>b[1]) return a[1] - b[1];
//Sorting the sorted records for 5th Column Yes/No Yes records are before No records
if (a[8]<b[8]) return 1;
if (a[8]>b[8]) return -1 ;
//Sorting the 5th Column after sorting 1st/5th column records
if (a[5]<b[5]) return b[5] - a[5];
if (a[5]>b[5]) return b[5] - a[5];
return 0;
}
April 29,
what are the sorting method
Can you give me the list of it??
May 1,
what are the sorting method
Can you give me the list of it??
helen
May 1,
Hi Helen,
I have implemented numerical and Alphabetical sorts . Call them in the Order you need . It Works.....
Call the sorting method from your Grid and specify the columns on which you need the sorting.
scoresGrid.sort(mysortfn);

May 2,
The new 1.0.1 release has improved 'stable' sorting algorithm

http://www.activewidgets.com/general.faq.releases/1-0-1.html
Alex (ActiveWidgets)
May 11,
Here it is a modified merge function to implemented numerical and date sorts:

// --------------------------------------------------------------
// 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
var fecha1, fecha2, cmp = new Array(3), igual = new Array(3);

while (lowPtr <= mid && highPtr <= upperBound)
{
if (direction == "ascending")
{

// Comprobar si el dato a ordenar es una fecha
fecha1 = arrayToCompare[theArray[lowPtr]].split("/");
if(arrayToCompare[theArray[lowPtr]].length == 11 && fecha1.length == 3){ // lo es
fecha2 = arrayToCompare[theArray[highPtr]].split("/");
cmp[0] = eval(fecha1[0] < fecha2[0]) // dia
cmp[1] = eval(fecha1[1] < fecha2[1]) // mes
cmp[2] = eval(fecha1[2] < fecha2[2]) // aÃ±o
igual[0] = eval(fecha1[0] == fecha2[0]) // dia
igual[1] = eval(fecha1[1] == fecha2[1]) // mes
igual[2] = eval(fecha1[2] == fecha2[2]) // aÃ±o

if (igual[2]){

if (igual[1]){
if (cmp[0])
workSpace[j++] = theArray[lowPtr++];
else
workSpace[j++] = theArray[highPtr++];
}
else{
if (cmp[1])
workSpace[j++] = theArray[lowPtr++];
else
workSpace[j++] = theArray[highPtr++];
}
}
else{
if (cmp[2])
workSpace[j++] = theArray[lowPtr++];
else
workSpace[j++] = theArray[highPtr++];
}
}
else{ // resto
if (arrayToCompare[theArray[lowPtr]] <= arrayToCompare[theArray[highPtr]])
workSpace[j++] = theArray[lowPtr++];
else
workSpace[j++] = theArray[highPtr++];
}
}
else
{
// Comprobar si el dato a ordenar es una fecha
fecha1 = arrayToCompare[theArray[lowPtr]].split("/");
if(arrayToCompare[theArray[lowPtr]].length == 11 && fecha1.length == 3){ // lo es
fecha2 = arrayToCompare[theArray[highPtr]].split("/");
cmp[0] = eval(fecha1[0] > fecha2[0]) // dia
cmp[1] = eval(fecha1[1] > fecha2[1]) // mes
cmp[2] = eval(fecha1[2] > fecha2[2]) // aÃ±o
igual[0] = eval(fecha1[0] == fecha2[0]) // dia
igual[1] = eval(fecha1[1] == fecha2[1]) // mes
igual[2] = eval(fecha1[2] == fecha2[2]) // aÃ±o

if (igual[2]){
if (igual[1]){
if (cmp[0])
workSpace[j++] = theArray[lowPtr++];
else
workSpace[j++] = theArray[highPtr++];
}
else{
if (cmp[1])
workSpace[j++] = theArray[lowPtr++];
else
workSpace[j++] = theArray[highPtr++];
}
}
else{
if (cmp[2])
workSpace[j++] = theArray[lowPtr++];
else
workSpace[j++] = theArray[highPtr++];
}
}
else{
if (arrayToCompare[theArray[lowPtr]] >= arrayToCompare[theArray[highPtr]])
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];
}
rosa
June 15,
Hello

How can use it in a jsp ??
Alex T
September 21,

This topic is archived.