要求:策划对于列表中显示的内容需要进行排序,比如先按人物等级从高到底,如果等级相同,则按贡献度从高到底,如果贡献度相同,则按id进行排序
实现:
public enum SortType
{
None,
Ascend,
Descend,
}
public class SortFieldData
{
//排序字段的名字
public string name;
//排序类型
public SortType sortType;
}
private static int CompareValue<T>(T x, T y, List<SortFieldData> sortFieldList, int index)
{
if (index >= sortFieldList.Count)
{
return 0;
}
if (sortFieldList[index] == null)
{
return 0;
}
Type xType = x.GetType();
Type yType = y.GetType();
FieldInfo xFieldInfo = xType.GetField(sortFieldList[index].name);
FieldInfo yFieldInfo = yType.GetField(sortFieldList[index].name);
if (xFieldInfo == null || yFieldInfo == null)
{
return 0;
}
long xValue = 0;
long yValue = 0;
if (xFieldInfo.FieldType == typeof(long))
{
long xValue2 = (long)xFieldInfo.GetValue(x);
long yValue2 = (long)yFieldInfo.GetValue(y);
xValue = xValue2;
yValue = yValue2;
}
else if (xFieldInfo.FieldType == typeof(bool))
{
bool xBool = (bool)xFieldInfo.GetValue(x);
bool yBool = (bool)yFieldInfo.GetValue(y);
xValue = xBool ? 1 : 0;
yValue = yBool ? 1 : 0;
}
else
{
int xValue1 = (int)xFieldInfo.GetValue(x);
int yValue1 = (int)yFieldInfo.GetValue(y);
xValue = xValue1;
yValue = yValue1;
}
//相等则进入下一层
if (xValue == yValue)
{
return CompareValue(x, y, sortFieldList, index + 1);
}
//不等比较
switch (sortFieldList[index].sortType)
{
case SortType.Ascend:
{
return xValue.CompareTo(yValue);
}
case SortType.Descend:
{
return yValue.CompareTo(xValue);
}
}
return 0;
}
private static void InternalSort<T>(List<T> list, List<SortFieldData> sortFieldList)
{
if (list == null)
{
return;
}
list.Sort((x, y) =>
{
return CompareValue(x, y, sortFieldList, 0);
});
}
public static void CustomSort<T>(List<T> list, List<SortFieldData> sortFieldList)
{
if (list == null)
{
return;
}
if (sortFieldList == null || sortFieldList.Count <= 0)
{
return;
}
if (sortFieldList[0] == null)
{
return;
}
//判断是否排序
if (!IsSort(list, sortFieldList))
{
return;
}
//开始排序
switch (sortFieldList[0].sortType)
{
case SortType.Ascend:
{
InternalSort(list, sortFieldList);
sortFieldList[0].sortType = SortType.Descend;
}
break;
case SortType.Descend:
{
InternalSort(list, sortFieldList);
sortFieldList[0].sortType = SortType.Ascend;
}
break;
}
}
//判断是否应该排序
private static bool IsSort<T>(List<T> list, List<SortFieldData> sortFieldList)
{
if (list == null || sortFieldList == null)
{
return false;
}
//遍历所有字段
foreach (SortFieldData item in sortFieldList)
{
if (item != null)
{
if (InternalIsSort(list, item.name))
{
return true;
}
}
}
return false;
}
//指定字段是否相同
private static bool InternalIsSort<T>(List<T> list, string targetName)
{
if (list == null)
{
return false;
}
for (int i = 0; i + 1 < list.Count; i++)
{
if (list[i] != null && list[i + 1] != null)
{
Type xType = list[i].GetType();
Type yType = list[i + 1].GetType();
FieldInfo xFieldInfo = xType.GetField(targetName);
FieldInfo yFieldInfo = yType.GetField(targetName);
if (xFieldInfo == null || yFieldInfo == null)
{
return false;
}
long xValue = 0;
long yValue = 0;
if (xFieldInfo.FieldType == typeof(int))
{
int xValue1 = (int)xFieldInfo.GetValue(list[i]);
int yValue1 = (int)yFieldInfo.GetValue(list[i + 1]);
xValue = xValue1;
yValue = yValue1;
}
else if (xFieldInfo.FieldType == typeof(long))
{
long xValue2 = (long)xFieldInfo.GetValue(list[i]);
long yValue2 = (long)yFieldInfo.GetValue(list[i + 1]);
xValue = xValue2;
yValue = yValue2;
}
else if (xFieldInfo.FieldType == typeof(bool))
{
bool xBool = (bool)xFieldInfo.GetValue(list[i]);
bool yBool = (bool)yFieldInfo.GetValue(list[i + 1]);
long xValue2 = xBool ? 1 : 0;
long yValue2 = yBool ? 1 : 0;
xValue = xValue2;
yValue = yValue2;
}
if (xValue != yValue)
{
return true;
}
}
}
return false;
}
调用:
public void SortFriendList()
{
List<SortFieldData> sortList = new List<SortFieldData>();
SortFieldData sortData;
//mGameFriendList中元素的donateSort字段从高到低排序
sortData = new SortFieldData();
sortData.name = "donateSort";
sortData.sortType = SortType.Descend;
sortList.Add(sortData);
//mGameFriendList中元素的lv字段从高到低排序
sortData = new SortFieldData();
sortData.name = "lv";
sortData.sortType = SortType.Descend;
sortList.Add(sortData);
//mGameFriendList中元素的friend_id字段从高到低排序
sortData = new SortFieldData();
sortData.name = "friend_id";
sortData.sortType = SortType.Descend;
sortList.Add(sortData);
for (int i = 0; i < mGameFriendList.Count; i++)
{
SetDonateSort(mGameFriendList[i]);
}
GlobalFunction.CustomSort(mGameFriendList, sortList);
}