叫参照排序我也不知道对错,先这样叫着吧。
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次,这个数据我觉得只能大概的反映一下效率,因为有时候数据的特殊性我也不好说.
这段时间没什么事做就看看数据结构,后面这篇博客都用这个例子,呵呵,就是练练手而已~~~