日期:2009-07-19  浏览次数:20866 次

1,javascript 中的排序
<script >
function KeyValue(serialkey,value)
{
this.serialkey = serialkey;
this.value = value;
}
function sortfunction(x,y)
{
return x.serialkey -y.serialkey;

}
var a=new Array(3);
a[0]=new KeyValue(30,'aaa');
a[1]=new KeyValue(10,'ddd');
a[2]=new KeyValue(20,'ccc');
a.sort(sortfunction);
window.alert(a[1].value);
</script>

2,在C#中的排序

using System;

namespace webUserWindowExample.test
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class KeyAndValue:System.IComparable
{
private int _key;
private string _value;

public int Key
{
get{return this._key;}
}

public string Value
{
get{return this._value ;}
}


public KeyAndValue(int key,string svalue)
{
_key=key;
_value=svalue;
}


public int CompareTo(object obj)
{
if(obj is KeyAndValue)
{
KeyAndValue temp = (KeyAndValue) obj;

return _key.CompareTo(temp.Key );
}

throw new ArgumentException("object is not a Temperature");
}


}
}

在webform1 page_load()中

webUserWindowExample.test.KeyAndValue [] a=new webUserWindowExample.test.KeyAndValue[5];

a[0]=new webUserWindowExample.test.KeyAndValue(30,"aaa");
a[1]=new webUserWindowExample.test.KeyAndValue(28,"bbb");
a[2]=new webUserWindowExample.test.KeyAndValue(37,"ccc");
a[3]=new webUserWindowExample.test.KeyAndValue(23,"ddd");
a[4]=new webUserWindowExample.test.KeyAndValue(35,"eee");

Array.Sort(a);

foreach(webUserWindowExample.test.KeyAndValue kv in a)
{
Response.Write(kv.Value);
}