我正在使用以下类按type
成员对列表进行排序:
public class CameraSortByType : IComparer<Camera>
{
private bool asc;
public CameraSortByType(bool a)
{
this.asc = a;
}
public int Compare(Camera x, Camera y)
{
if (x.type > y.type)
return asc? -1 : 1;
if (x.type < y.type)
return asc? 1 : -1;
else
return 0;
}
}
我还按以下方式对相同的列表进行排序name
:
myList.Sort((s1, s2) => s2.name.CompareTo(s1.name));
如何将按名称排序合并到按类型排序的类中?所以当我按类型排序时,它也按名称排序?
更新:Linq 版本
var primarySortResult = primarySort ? CameraStorage.CameraList.OrderBy(x => x.type) : CameraStorage.CameraList.OrderByDescending(x => x.type);
var secondarySortResult = secondarySort ? primarySortResult.ThenBy(x => x.name) : primarySortResult.ThenByDescending(x => x.name);
CameraStorage.CameraList = secondarySortResult.ToList();