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); }