public class PlayInfo
{
public string name;
public int power;
public int type;
public int brains;
public PlayInfo(string s,int p,int t,int b)
{
name = s;
power = p;
type = t;
brains = b;
}
/// <summary>
/// 小于0,放在传入对象的前面,等于0,保持当前的位置不变,大于0,放入传入对象的后面
/// </summary>
public static int Compare(PlayInfo p,PlayInfo p1)
{
if (p.power > p1.power)
{
return -1;
}
else if (p.power == p1.power)
{
if (p.brains > p1.brains)
{
return -1;
}
else if(p.brains < p1.brains)
{
return 1;
}
else
{
if (p.type > p1.type)
{
return 1;
}
else if(p.type<p1.type)
{
return -1;
}
else
{
return 0;
}
}
}
else
{
return 1;
}
}
}
public class playSort : MonoBehaviour
{
private List<PlayInfo> infos = new List<PlayInfo>();
private PlayInfo p1 = new PlayInfo("诸葛亮", 30, 5, 100);
private PlayInfo p2 = new PlayInfo("关羽", 96, 1, 75);
private PlayInfo p3 = new PlayInfo("吕布", 100, 2, 70);
private PlayInfo p4 = new PlayInfo("项羽", 100, 2, 72);
private PlayInfo p5 = new PlayInfo("张良", 30, 4, 100);
// Start is called before the first frame update
void Start()
{
infos.Add(p1);
infos.Add(p2);
infos.Add(p3);
infos.Add(p4);
infos.Add(p5);
infos.Sort(PlayInfo.Compare);
foreach (var item in infos)
{
Debug.Log(item.name);
}
}
}
Unity list.Sort自定义排序
最新推荐文章于 2024-02-18 13:49:26 发布