实体类
public class Person { public string Name { get; set; } public int Age { get; set; } public Person(string name, int age) { Name = name; Age = age; } }
排序
List<Person> personArr = new List<Person>(); personArr.Add(new Person("BAIQJH", 18)); personArr.Add(new Person("EAIGG", 20)); personArr.Add(new Person("AAICC", 19)); //按名字排序 personArr.Sort(new Comparison<Person>(delegate(Person p1, Person p2) { return p1.Name.CompareTo(p2.Name); })); //按年龄排序 personArr.Sort(new Comparison<Person>(delegate(Person p1, Person p2) { return p1.Age - p2.Age; })); //显示结果 foreach (var item in personArr) { textBox1.Text += item.Name + " " + item.Age + Environment.NewLine; }
结果
//按名字排序结果 AAICC 19 BAIQJH 18 EAIGG 20 //按年龄排序结果 BAIQJH 18 AAICC 19 EAIGG 20