ArrayList可以对元素进行排序,但是要实现ICompare接口 楼主参数一下: //实现ICompare接口 public class MySort : IComparer { // Calls CaseInsensitiveComparer.Compare with the parameters reversed. int IComparer.Compare(Object x, Object y) { int i = Convert.ToInt32(x); int j = Convert.ToInt32(y); if (i > j) return 1; else if (i < j) return -1; else return 0; } } ArrayList a = new ArrayList(); a.Add(10); a.Add(55); a.Add(12); a.Add(14); a.Add(40); IComparer compare = new MySort(); a.Sort(compare); foreach (object o in a) { Console.WriteLine(o.ToString()); }
1. 页面后台代码:protected void Page_Load(object sender, EventArgs e) { ExtendPerson scott = new ExtendPerson("Scott", "Hanselman"); ExtendPerson bill = new ExtendPerson("Bill", "Evjen"); ExtendPerson srini = new ExtendPerson("Srinivasa", "Sivakumar"); ArrayList people = new ArrayList(); people.Add(scott); people.Add(bill); people.Add(srini); Response.Write("We used foreach:<BR/>"); foreach (ExtendPerson p in people) { Response.Write(p.FullName + "<BR/>"); } Response.Write("<br>Sort...<BR><br>"); people.Sort(); Response.Write("We used foreach:<BR/>"); foreach (ExtendPerson p in people) { Response.Write(p.FullName + "<BR/>"); } ExtendPerson scott2 = new ExtendPerson("Scott", "Hanselman"); int indexOfScott2 = people.IndexOf(scott2); Response.Write("<br><br>Scott #2 is at " + indexOfScott2 + "<BR/>"); int indexOfEquivalentScott = people.BinarySearch(scott2); Response.Write("An Equivalent Scott is at " + indexOfEquivalentScott + "<BR/>"); Response.Write("<br>We used a for loop.<BR/>"); for (int i = 0; i < people.Count; i++) { Response.Write(((ExtendPerson)people[i]).FullName + "<BR/>"); } } 2.实体类public class Person { private string FirstName; private string LastName; public Person(string first, string last) { FirstName = first; LastName = last; } public string FullName { get { return FirstName + " " + LastName; } } }
可以用SortedList 能对键排序
提示下把你的要排序数据做为键值示例代码如下:System.Collections.SortedList sl=new SortedList(); sl.Add(10,1); sl.Add(20,2); sl.Add(14,3); string s=""; foreach(System.Collections.DictionaryEntry de in sl) { s+=de.Value.ToString()+"<br>";
楼主参数一下:
//实现ICompare接口
public class MySort : IComparer
{
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare(Object x, Object y)
{
int i = Convert.ToInt32(x);
int j = Convert.ToInt32(y); if (i > j)
return 1;
else if (i < j)
return -1;
else return 0;
}
} ArrayList a = new ArrayList();
a.Add(10);
a.Add(55);
a.Add(12);
a.Add(14);
a.Add(40); IComparer compare = new MySort(); a.Sort(compare); foreach (object o in a)
{
Console.WriteLine(o.ToString());
}
{
ExtendPerson scott = new ExtendPerson("Scott", "Hanselman");
ExtendPerson bill = new ExtendPerson("Bill", "Evjen");
ExtendPerson srini = new ExtendPerson("Srinivasa", "Sivakumar"); ArrayList people = new ArrayList();
people.Add(scott);
people.Add(bill);
people.Add(srini); Response.Write("We used foreach:<BR/>");
foreach (ExtendPerson p in people)
{
Response.Write(p.FullName + "<BR/>");
} Response.Write("<br>Sort...<BR><br>");
people.Sort(); Response.Write("We used foreach:<BR/>");
foreach (ExtendPerson p in people)
{
Response.Write(p.FullName + "<BR/>");
} ExtendPerson scott2 = new ExtendPerson("Scott", "Hanselman");
int indexOfScott2 = people.IndexOf(scott2);
Response.Write("<br><br>Scott #2 is at " + indexOfScott2 + "<BR/>"); int indexOfEquivalentScott = people.BinarySearch(scott2);
Response.Write("An Equivalent Scott is at " + indexOfEquivalentScott +
"<BR/>"); Response.Write("<br>We used a for loop.<BR/>");
for (int i = 0; i < people.Count; i++)
{
Response.Write(((ExtendPerson)people[i]).FullName + "<BR/>");
}
}
2.实体类public class Person
{
private string FirstName;
private string LastName; public Person(string first, string last)
{
FirstName = first;
LastName = last;
} public string FullName
{
get
{
return FirstName + " " + LastName;
}
}
}
sl.Add(10,1);
sl.Add(20,2);
sl.Add(14,3);
string s="";
foreach(System.Collections.DictionaryEntry de in sl)
{
s+=de.Value.ToString()+"<br>";
}
Response.Write(s);1
3
2