unity的list列表操作(结合Linq整理的一些高级写法Sort、Where等)

   简单的就不介绍了,增删改查等都是最基础的,下面讲一些高级写法和实用的

1、List.Sort()

Unity的List.Sort有三种结果 1,-1,0分别是大,小,相等。默认List的排序是升序排序,如果要降序排序,也很简单,只需要在前面加一个负号即可。

List<int> m_temp = new List<int>(){6,1,3,5,4};
//  升序
m_temp.Sort((x, y) => x.CompareTo(y));
// 降序
m_temp.Sort((x, y) => -x.CompareTo(y));
Console.WriteLine(m_temp);
// 6,5,4,3,1

2、2个列表比较
    //对比相等(顺序也要相同)
        List<int> listA1 = new List<int> { 1, 2, 3, 4, 5, 6 };
        List<int> listA2 = new List<int> { 1, 2, 3, 4, 5, 6 };

        //SequenceEqual方法需引用using System.Linq;
        if (listA1.SequenceEqual(listA2))
        {
            Debug.Log("A1==A2");
        }
        else
        {

猜你喜欢

转载自blog.csdn.net/qq_39646949/article/details/128022183