void Swap(ref object x, ref object y ) {
object temp = null;
temp = x;
x = y;
y = temp;
}
}
测试代码 UserClass
public class UserClass {
protected string _name;
protected int _age;
protected string _address;
public UserClass(string _name, int _age, string _address) {
this._name = _name;
this._age = _age;
this._address = _address;
}
public string Name {
get { return _name; }
set { _name = value; }
}
public int Age {
get { return _age; }
set { _age = value; }
}
public string Address {
get { return _address; }
set { _address = value; }
}
}
main
其中ReverserClass reverser = new ReverserClass(user.GetType(), " Name " ); 中的要传递ArrayList数组中的对象的类型和需要作为排序依据的字段名称!
static void Main( string [] args)
{
UserClass user;
ArrayList ar = new ArrayList();
user = new UserClass("1", 1, "1");
ar.Add(user);
user = new UserClass("9", 9, "9");
ar.Add(user);
user = new UserClass("4", 4, "4");
ar.Add(user);
user = new UserClass("3", 3, "3");
ar.Add(user);
user = new UserClass("6", 6, "6");
ar.Add(user);
Console.WriteLine();
ReverserClass reverser = new ReverserClass(user.GetType(), "Name", "DESC");
ar.Sort(reverser);
Console.WriteLine();