在js和cs中的排序

王朝asp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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);

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航