方法相关 参数: string数组 - 全部要比较的字段名称 bool数组 - 每一个字段升序排序还是降序排序 IList<T>集合 - 要排序的List
内部实现; 通过反射找到全部string数组中的字段,从第一个字段开始比较,如果相等,比较第二个字段,直到最后一个,通过bool控制升序还是降序。
使用方法 如Main函数中的使用,传入参数,调用IListSort类中的Sort方法,得到的传入的list就会进行排序。
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<Person> personList = new List<Person>
{
new Person("kk",101,150,true),
new Person("kklady",100,10,true),
new Person("kkwomen",100,2,true),
new Person("kkwomen",100,777,true),
new Person("kkwomen",712,150,true),
new Person("kkwomen",99,666,true),
};
string[] field = { "age", "height" };
bool[] boolArray = { true, false };
IListSort<Person> listSort = new IListSort<Person>(field, boolArray, personList);
listSort.Sort();
Console.WriteLine(personList);
}
}
public class IListSort<T>
{
private string[] propertName;
private bool[] sortBy;
private IList<T> iList;
public IListSort(string[] propertName, bool[] sortBy, IList<T> iList)
{
this.propertName = propertName;
this.sortBy = sortBy;
this.iList = iList;
}
public void Sort()
{
if (iList.Count <= 1)
{
return;
}
for (int i = 1; i < iList.Count; i++)
{
T t = iList[i];
int j = i;//避免闭包
while ((j > 0) && Compare(iList[j - 1], t) < 0)
{
iList[j] = iList[j - 1];
--j;
}
iList[j] = t;
}
return;
}
/// <summary>
/// 比较大小,前者小于后者返回1,相等返回0,前者大于后者返回-1
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t1"></param>
/// <param name="t2"></param>
/// <returns></returns>
public int Compare(T t1, T t2)
{
for (int i = 0; i < propertName.Length; i++)
{
if (string.IsNullOrEmpty(propertName[i]))
{
throw new ArgumentNullException("空排序属性异常");
}
}
FieldInfo[] fieldInfos = new FieldInfo[propertName.Length];
var type = typeof(T);
for (int i = 0; i < propertName.Length; i++)
{
FieldInfo fieldInfo = type.GetField(propertName[i]);
if (fieldInfo == null)
{
throw new ArgumentNullException(propertName[i] + "字段不存在");
}
fieldInfos[i] = fieldInfo;
}
int compare = 0;
for (int i = 0; i < propertName.Length; i++)
{
//compare等于一个排序方法
compare = CompareInField(t1, t2, fieldInfos[i], sortBy[i]);
if(compare != 0)//控制
{
return compare;
}
}
return compare;
}
private int CompareInField(T t1, T t2, FieldInfo fieldInfo, bool sortBy)
{
switch (fieldInfo.FieldType.ToString())
{
case "System.Int32"://这里是等于FieldType的全名
int int1 = 0;
int int2 = 0;
if (fieldInfo.GetValue(t1) != null)
{
int1 = Convert.ToInt32(fieldInfo.GetValue(t1));
}
if (fieldInfo.GetValue(t2) != null)
{
int2 = Convert.ToInt32(fieldInfo.GetValue(t2));
}
if (sortBy)
{
return int2.CompareTo(int1);//升序
}
else
{
return int1.CompareTo(int2);//降序
}
case "System.Double":
double double1 = 0;
double double2 = 0;
if (fieldInfo.GetValue(t1) != null)
{
double1 = Convert.ToDouble(fieldInfo.GetValue(t1));
}
if (fieldInfo.GetValue(t2) != null)
{
double2 = Convert.ToDouble(fieldInfo.GetValue(t2));
}
if (sortBy)
{
return double2.CompareTo(double1);
}
else
{
return double1.CompareTo(double2);
}
case "string":
default:
break;
}
return 0;
}
}
public class Person
{
public string name;
public int age;
public double height;
public bool sex;
public Person(string name, int age, double height, bool sex)
{
this.name = name;
this.age = age;
this.height = height;
this.sex = sex;
}
}