<script language=javascript>
function File(filename,filesize)
{
this.filename=filename;
this.filesize=filesize;
}
function ArrayFile()
{
this.f = new Array();
this.count=0;
this.add=function(file)
{
this.f.push(file);
this.count+=1;
}
this.max=function(f1, f2, field)
{
switch (field)
{
case "filename":
return f1.filename>f2.filename? true:false;
case "filesize":
return f1.filesize>f2.filesize? true:false;
default:
return false;
}
}
this.sort=function(field,order)
{
//order:desc asc
//field:filename filesize
var len = this.f.length;
if( len < 2 ) return;
var tmp;
for(var i=0; i<len-1; i++)
{
for(var j=i+1; j<len; j++)
{
if( order == "desc")
{
if( !this.max(this.f[i], this.f[j], field) )
{
tmp = this.f[i];
this.f[i] = this.f[j];
this.f[j] = tmp;
}
}
else if ( order == "asc")
{
if( this.max(this.f[i], this.f[j], field) )
{
tmp = this.f[i];
this.f[i] = this.f[j];
this.f[j] = tmp;
}
}
}
}
}
}
var t1 = new File("c.asp",32);
var t2 = new File("a.asp",132);
var t3 = new File("b.asp",12);
var ArrayFile = new ArrayFile();
ArrayFile.add(t1);
ArrayFile.add(t2);
ArrayFile.add(t3);
ArrayFile.sort("filesize","asc");
alert(ArrayFile.f[0].filename);
alert(ArrayFile.f[1].filename);
alert(ArrayFile.f[2].filename);
</script>
本文地址:http://com.8s8s.com/it/it7497.htm