排序(1)c#参照排序

叫参照排序我也不知道对错,先这样叫着吧。

public class PlayerProperty
{
	//玩家信息.
	public class Player
	{
		public int uid;//玩家id.
		public string name;//玩家姓名.
		public byte level;//玩家等级.
		public int gold;//玩家金币.
	}
	static PlayerProperty m_singleton = null;
	public static PlayerProperty Singleton
	{
		get{
			if(m_singleton == null)
			{
				m_singleton = new PlayerProperty();
			}
			return m_singleton;
		}
	}
	
	//public Dictionary<ushort,Player> m_playerDict = new Dictionary<ushort,Player>();
	public List<Player> m_playerList = new List<Player>();
	PlayerProperty()
	{
		LoadPlayerMsg();
	}
	void LoadPlayerMsg()
	{
		string path;
		path = "PlayerMsg";
		string[] lines = (Resources.Load(path) as TextAsset).text.Split(new char[] { '\r', '\n' }, System.StringSplitOptions.RemoveEmptyEntries);
		int count = lines.Length;
		for(int i = 0;i < count; i++)
		{
			if(i == 0)
			{
				continue;
			}
			string[] splits = lines[i].Split(new char[] {','});
			Player player = new Player();
			player.uid = int.Parse(splits[0]);
			player.name = splits[1];
			player.level = byte.Parse(splits[2]);
			player.gold = int.Parse(splits[3]);
			//m_playerDict.Add(player.uid,player);
			m_playerList.Add(player);
		}
	}
	int recNum ;
	/// <summary>
	/// c#的排序.
	/// </summary>
	public void SortMethod1()
	{	
		recNum = 0;
		m_playerList.Sort(SortLevel);
		OutPutMsg();
	}
	int SortLevel(Player p1,Player p2)
	{	
		recNum++;
		Debug.Log("SortMethod1-----------"+recNum);
		return p2.level.CompareTo(p1.level);
	}
	void OutPutMsg()
	{

		foreach(Player player in m_playerList)
		{
			Debug.Log(player.uid+"--"+player.name+"--"+player.level+"--"+player.gold);
		}
	}
}

Test.cs:

public class DataTest : MonoBehaviour {
	void Start () {
		PlayerProperty.Singleton.SortMethod1();
	}
	
}



表数据:

排序后打印列表:

循环排了20次,这个数据我觉得只能大概的反映一下效率,因为有时候数据的特殊性我也不好说.

这段时间没什么事做就看看数据结构,后面这篇博客都用这个例子,呵呵,就是练练手而已~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值