结构数组按特定顺序排序的可能性,高手进
C# code
public struct ChanZiInfo
{
public string MapName;
public int TarX;
public int TarY;
}
ChanZiInfo[] CZInfo = new ChanZiInfo[10];
CZInfo[0].MapName = "长春";
CZInfo[0].TarX = 100;
CZInfo[0].TarY = 200;
CZInfo[1].MapName = "沈阳";
CZInfo[1].TarX = 125;
CZInfo[1].TarY = 658;
CZInfo[0].MapName = "长春";
CZInfo[0].TarX = 512;
CZInfo[0].TarY = 998;
.
.
.
.
.
.
CZInfo[0].MapName = "上海";
CZInfo[0].TarX = 123;
CZInfo[0].TarY = 635;
这样的一个结构, 如何能让这个结构数组 按 MapName 排序?
------解决方案--------------------
CZInfo = CZInfo.OrderBy(x => MapName).ToArray();
------解决方案--------------------
没有linq和lambda,也可以用IComparer接口,然后用动态数组List.Sort(IComparer)排序,不过比较麻烦,
public struct ChanZiInfo
{
public string MapName;
public int TarX;
public int TarY;
public class Cmp:IComparer<ChanZiInfo>
{
public int Compare(ChanZiInfo x,ChanZiInfo y)
{
return x.MapName.CompareTo(y.MapName);
}
}
}
排序的时候:
List<ChanZiInfo> CZInfo=new List<ChanZiInfo>();
....
CZInof.Sort(new ChanZiInfo.Cmp);//这样就可以排序,