using UnityEngine; using System.Collections.Generic ; using System; //集合或数组的助手类 public static class CollectionHelper { //升序排列 public static void OrderBy(T[] array,SelectHandler handler) where TKey : IComparable { for (int i = 0; i < array.Length -1; i++) for (int j = i+1; j < array.Length ; j++) if (handler(array[i]).CompareTo(handler(array[j])) > 0) { var temp = array[i]; array[i] = array[j]; array[j] = temp; } } //降序排列 public static void OrderByDescending(T[] array, SelectHandler handler) where TKey : IComparable { for (int i = 0; i < array.Length - 1; i++) for (int j = i + 1; j < array.Length; j++) if (handler(array[i]).CompareTo(handler(array[j])) < 0) { var temp = array[i]; array[i] = array[j]; array[j] = temp; } } public delegate bool FindHandler(T item); public delegate TKey SelectHandler(TSource source); //查找 public static T Find(T[] array, FindHandler handler) { foreach (var item in array) { //调用委托 if (handler(item)) return item; } return default(T); } //查找 public static T Find(List list, FindHandler handler) { foreach (var item in list) { //调用委托 if (handler(item)) return item; } return default(T); } //查找 public static T[] FindAll(T[] array, FindHandler handler) { List tempList = new List(); foreach (var item in array) { //调用委托 if (handler(item)) tempList.Add(item); } return tempList.Count > 0 ? tempList.ToArray() : null; } public static TKey[] Select(T[] array, SelectHandler handler) { TKey[] tempArr = new TKey[array.Length]; for (int i = 0; i < array.Length ; i++) { tempArr[i] =handler(array[i]); } return tempArr; } }