//排序
var newFaceInfoList = faceTopInfoList.OrderByDescending(o => o.finalValue).ToList();//降序 (更新排行榜的infoList的排序)

var quChongList= faceTopInfoList.Where((x, i) => faceTopInfoList.FindIndex(z => z.id == x.id) == i).ToList();//Lambda表达式去重  通过face.id去重

 

//去重

public List<int> getNewList(List<int> li)
{
    //List<string> list = new ArrayList<string>();
    List<int> list = new List<int>();
    for (int i = 0; i < li.Count; i++)
    {
        int n = li[i];  //获取传入集合对象的每一个元素
        if (!list.Contains(n))
        {   //查看新集合中是否有指定的元素,如果没有则加入
            list.Add(n);
        }
    }
    return list;  //返回集合
}

 

 

根据对象属性值 获取索引(未测试)

List<Person> people = new List<Person>
{
new Person { Name = "Alice", Age = 30 },
new Person { Name = "Bob", Age = 25 },
new Person { Name = "Charlie", Age = 35 }
};
 
int index = people.FindIndex(p => p.Name == "Bob");
 
 

List<T>把指定元素放到首位或末尾

public static void ElementToFirst<T>(this List<T> self, T element)

        {

            if (self.Contains(element))

            {

                int index = self.IndexOf(element);

                for (int i = index; i > 0; i--)

                {

                    self[i] = self[i - 1];

                }

                self[0] = element;

            }

        }

        public static void ElementToLast<T>(this List<T> self, T element)

        {

            if (self.Contains(element))

            {

                int index = self.IndexOf(element);

                int lastIndx = self.Count - 1;

                for (int i = index; i < lastIndx; i++)

                {

                    self[i] = self[i + 1];

                }

                self[lastIndx] = element;

            }

        }

 

将指定索引的元素移动list末尾

public void ElementToLast(List<int> self, int index)

{
    int element = self[index];//先存储需要调到末尾的元素
    int lastIndx = self.Count - 1;

    for (int i = index; i < lastIndx; i++)

    {
        self[i] = self[i + 1];

    }

    self[lastIndx] = element;

}

//调用方式
ElementToLast(intList,2);

 

删除从indexToRemove到List末尾的所有元素

List<int> list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        int indexToRemove = 4; // 指定要从哪个索引开始删除

        // 删除从indexToRemove到List末尾的所有元素
        list.RemoveRange(indexToRemove, list.Count - indexToRemove);
        foreach (var _i in list)
        {
            Debug.Log(_i);
        }

 

排序(OrderBy和ThenBy

List<Customer> list = new List<Customer>();
list.Add(new Customer { id = 1, name = "刘德华", age = 56, period=40 });
list.Add(new Customer { id = 2, name = "张学友", age = 52, period = 35 });
list.Add(new Customer { id = 3, name = "黎明", age = 58, period = 33 });
list.Add(new Customer { id = 4, name = "郭富城", age = 60, period = 36 });
list.Add(new Customer { id = 5, name = "古天乐", age = 60, period = 30 });
var sortlist = list.OrderBy(o=>o.age).ThenBy(t=>t.period).ToList();
//这是类
 public class Customer
{
        public int id { get; set; }
        public string name { getset; }
        public int age { get; set; }
        public int period { get; set; }
 }

 

使用比较函数排序

List<string> names = new List<string> { "刘德华""张飞""赵云""公众号:dotnet开发跳槽" };
names.Sort((x, y) => x.Length.CompareTo(y.Length));
    foreach (var name in names)
    {
      Console.WriteLine(name);
    }

 

使用Linq的OrderBy实现随机排序

List<Item> _tempItemList = itemList.OrderBy(x => Guid.NewGuid()).ToList();//随机排序

 

posted on 2024-03-26 17:01  凌落成迷  阅读(59)  评论(0)    收藏  举报