List 排序 功能 这两天又遇到要做排序功能 整理了一下 记录在案 方便以后使用
using System.Collections;
using System;
public class Sort_List_Test : IComparable
{
public float x;
public float y;
public float z;
public Object Object;
public int CompareTo(object obj)
{
int result=0;
try
{
Sort_List_Test sortObj = obj as Sort_List_Test;
if (this.z > sortObj.z)
{
result = 1;
}
else
{
result = -1;
}
return result;
}
catch (Exception ex) { throw new Exception(ex.Message); }
}
/// <summary>
///降序(仅针对第一优先级条件)
/// </summary>
public static int CompareOrderByDescending(Sort_List_Test a, Sort_List_Test b)
{
int result = 0;
if (a.x < b.x)
{
result = 1;
}
else if (a.x == b.x)
{
if (a.y < b.y)
{
result = 1;
}
else if (a.y == b.y)
{
if (a.z < b.z)
{
result = -1;
}
else
{
result = 1;
}
}
else
{
result = -1;
}
}
else
{
result = -1;
}
return result;
}
/// <summary>jing
///升序(仅针对第一优先级条件)
/// </summary>
public static int CompareOrderBy(Sort_List_Test a, Sort_List_Test b)
{
int result = 0;
if (a.x > b.x)
{
result = 1;
}
else if (a.x == b.x)
{
if (a.y < b.y)
{
result = 1;
}
else if (a.y == b.y)
{
if (a.z < b.z)
{
result = -1;
}
else
{
result = 1;
}
}
else
{
result = -1;
}
}
else
{
result = -1;
}
return result;
}
}
以三个条件逐一作为排序条件
private List SoulWeapon_Strip_Sort(List list_SoulWeapon_info)
{
List<Sort_List_Test> SortSoulWeapon = new List<Sort_List_Test>();
private List<SoulWeapon_Player_Info> SoulWeapon_Strip_Sort(List<SoulWeapon_Player_Info> list_SoulWeapon_info)
{
List<Sort_List_Test> SortSoulWeapon = new List<Sort_List_Test>();
foreach (SoulWeapon_Player_Info soulWeapon in list_SoulWeapon_info)
{
SortSoulWeapon.Add(new Sort_List_Test()
{
x = soulWeapon.Int_SoulWeapon_Quality,
y = soulWeapon.Int_SoulWeapon_Level,
z = soulWeapon.Int_SoulWeapon_Type, Object = soulWeapon });
}
SortSoulWeapon.Sort(Sort_List_Test.CompareOrderByDescending);
List<SoulWeapon_Player_Info> soulweapon_list =
new List<SoulWeapon_Player_Info>();
foreach (Sort_List_Test soulWeapon in SortSoulWeapon)
{
soulweapon_list.Add(soulWeapon.Object as SoulWeapon_Player_Info);
Debug.LogError("Quality:" + soulWeapon.x + " Level:" + soulWeapon.y + " Type:" + soulWeapon.z);
}
return soulweapon_list;
}