I wish to sort a table dynamically using array.sort method. Below is my code, but it didn't work, can any one give help ???
--------------- Script ----------------------
<SCRIPT LANGUAGE="JavaScript"><!--
function myObject(number,text,date,name) {
this.number = number;
this.text = text;
this.date = date;
this.name = name;
}
function setObject(number,text,date,name){
myObjectArray[objectArrayIndex++] = new myObject(number,text,date,name);
}
var objectArrayIndex = 0;
var myObjectArray = new Array();
setObject(100,'abc','1997/11/04','Me');
setObject(400,'zzz','1996/10/03','You');
setObject(300,'fox','1995/09/02','Them');
setObject(50,'bad','1998/12/01','Us');
setObject(110,'fbc','2000/11/04','We');
function Compare(a,b) {
for (var i in myObjectArray) {
return a[0]-b[0];
}
}
function showObjectArray(object,length) {
var output = '<CENTER><TABLE BORDER=1>';
output += '<TR>' +
'<TH><A HREF="test18.htm?number">number<\/A><\/TH>' +
'<TH><A HREF="test18.htm?text">text<\/A><\/TH>' +
'<TH><A HREF="test18.htm?date">date<\/A><\/TH>' +
'<TH><A HREF="test18.htm?name">name<\/A><\/TH>' +
'<\/TR>';
for (var i=0; i<length; i++)
output += '<TR>' +
'<TD>' + object.number+ '<\/TD>' +
'<TD>' + object.text + '<\/TD>' +
'<TD>' + object.date + '<\/TD>' +
'<TD>' + object.name + '<\/TD>' +
'<\/TR>';
output += '<\/TABLE><\/CENTER>';
document.write(output);
}
var sortProperty = window.location.search.substring(1);
if(sortProperty.length != 0)
myObjectArray.sort(Compare);
showObjectArray(myObjectArray,objectArrayIndex);
//--></SCRIPT>
------------- End Script --------------------
[sig][/sig]
--------------- Script ----------------------
<SCRIPT LANGUAGE="JavaScript"><!--
function myObject(number,text,date,name) {
this.number = number;
this.text = text;
this.date = date;
this.name = name;
}
function setObject(number,text,date,name){
myObjectArray[objectArrayIndex++] = new myObject(number,text,date,name);
}
var objectArrayIndex = 0;
var myObjectArray = new Array();
setObject(100,'abc','1997/11/04','Me');
setObject(400,'zzz','1996/10/03','You');
setObject(300,'fox','1995/09/02','Them');
setObject(50,'bad','1998/12/01','Us');
setObject(110,'fbc','2000/11/04','We');
function Compare(a,b) {
for (var i in myObjectArray) {
return a[0]-b[0];
}
}
function showObjectArray(object,length) {
var output = '<CENTER><TABLE BORDER=1>';
output += '<TR>' +
'<TH><A HREF="test18.htm?number">number<\/A><\/TH>' +
'<TH><A HREF="test18.htm?text">text<\/A><\/TH>' +
'<TH><A HREF="test18.htm?date">date<\/A><\/TH>' +
'<TH><A HREF="test18.htm?name">name<\/A><\/TH>' +
'<\/TR>';
for (var i=0; i<length; i++)
output += '<TR>' +
'<TD>' + object.number+ '<\/TD>' +
'<TD>' + object.text + '<\/TD>' +
'<TD>' + object.date + '<\/TD>' +
'<TD>' + object.name + '<\/TD>' +
'<\/TR>';
output += '<\/TABLE><\/CENTER>';
document.write(output);
}
var sortProperty = window.location.search.substring(1);
if(sortProperty.length != 0)
myObjectArray.sort(Compare);
showObjectArray(myObjectArray,objectArrayIndex);
//--></SCRIPT>
------------- End Script --------------------
[sig][/sig]