日期:2014-05-18  浏览次数:20771 次

this 方面的问题.
[code=C#][/code]
public enum enuSortOrder
{IDAsc, IDDesc, RankAsc, RankDesc}

public class Person : IComparable
{
public static enuSortOrder intSortOrder = enuSortOrder.IDAsc;

public int ID;
public string Rank;

public Person(int id, string rank)
{ this.ID=id; this.Rank = rank; }

public int CompareTo(object obj)
{
Person p = (Person)obj;
switch ((int)intSortOrder)
{
case (int)enuSortOrder.IDAsc:
return this.ID.CompareTo(p.ID);// 问题是this这里的具体值不断的发生变化,他是怎么发生变化的?
}
}

private int RankCompare(string rank1, string rank2)
{
...
}

private int ConvertRankToInt(string rank)
{
  ...
}
}

class SortArrayList 
{
static void Main(string[] args) 
{
ArrayList list = new ArrayList();
list.Add(new Person(6, "排长"));
list.Add(new Person(3, "团长"));
list.Add(new Person(4, "司令"));
list.Add(new Person(5, "旅长"));
list.Add(new Person(7, "连长"));
list.Add(new Person(1, "军长"));
list.Add(new Person(2, "营长"));
list.Add(new Person(8, "师长"));

list.Sort();
Console.WriteLine("Sort By ID Asc:");
foreach (Person person in list) 
{
Console.WriteLine("ID: " + person.ID.ToString() + ", Rank: " + person.Rank);
}
}
}

------解决方案--------------------
使用 QuickSort 算法
http://hi.baidu.com/jzinfo/blog/item/bd8d97cae1d15e86c91768c8.html