用什么集合来储存这样的数据?

来源:百度知道 编辑:UC知道 时间:2024/09/22 05:36:05
有一对应关系:中国(13亿),美国(3亿),德国(0.8亿),澳洲(0.2亿)。
要求能按人口数排序。不用数组,用什么集合来储存这样的数据呢?用ArrayList或 Hashtable或 SortedList?好像都不行,请教高手。

把你的对应关系定义成结构或类:以类为例
class Country
{
public Country(string pName,float pNumber)
{
this.Name = pName;
this.Number = pNumber;
}
public string Name;
public float Number;
}

Country con = new Country("中国",13);
Country con1 = new Country("美国",3);
.......
List<Country> list = new List<Country>();
list.Add(con);
list.Add(con1);
list.Sort(new MyCompare());//按人口排序;MyCompare定义如下:

关于排序,你可使用ICompare接口
public class MyCompare : IComparer<Country>
{
#region IComparer<Country> 成员

public int Compare(Country x, Country y)
{
if (x == null)
{
if (y == null)
return 0;
else
return -1;
}
else
{
if