您的位置:首页 > 编程语言 > Go语言

按不同国家语言进行字符串排序 选自:Goodspeed's Blog

2005-03-30 16:05 447 查看

void Page_Load(object sender, EventArgs e)






{


//测试数据


string[] myArr = new string[6];


myArr[0] = "地域";


myArr[1] = "地図";


myArr[2] = "路線";


myArr[3] = "道路交通";


myArr[4] = "電話帳";


myArr[5] = "自動車";




//没有排序


Gridview1.DataSource = myArr;


Gridview1.DataBind();




//简体中文排序


MyStringComparer myComp = new MyStringComparer(CompareInfo.GetCompareInfo("zh-CN"), CompareOptions.None);


Array.Sort(myArr, myComp);


Gridview2.DataSource = myArr;


Gridview2.DataBind();




//日语排序


myComp = new MyStringComparer(CompareInfo.GetCompareInfo("ja-JP"), CompareOptions.None);


Array.Sort(myArr, myComp);


Gridview3.DataSource = myArr;


Gridview3.DataBind();


}




private class MyStringComparer : IComparer






{


private CompareInfo myComp;


private CompareOptions myOptions = CompareOptions.None;




// Constructs a comparer using the specified CompareOptions.


public MyStringComparer(CompareInfo cmpi, CompareOptions options)






{


myComp = cmpi;


this.myOptions = options;


}




// Compares strings with the CompareOptions specified in the constructor.


public int Compare(Object a, Object b)






{


if (a == b) return 0;


if (a == null) return -1;


if (b == null) return 1;




string sa = a.ToString();


string sb = b.ToString();


if (sa != null && sb != null)


return myComp.Compare(sa, sb, myOptions);


throw new ArgumentException("a and b should be strings.");




}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: