2

我正在使用以下类按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();
4

3 回答 3

1

如果 LINQ 是一个选项,您可以使用以下运算符来创建有序序列:

样本:

var orderedList = myList.OrderBy(x => x.type)
                        .ThenByDescending(x => x.name)
                        .ToList();

如果您需要根据某些条件订购:

var result = ascendingByType ? myList.OrderBy(x => x.type) :
                               myList.OrderByDescending(x => x.type);

if (orderByNameRequired)
{
    result = ascendingByName ? result.ThenBy(x => x.name) :
                               result.ThenByDescending(x => x.name);
}

orderedList = result.ToList();

还可以考虑使用Dynamic Linq


这里还有一个比较器实现,用于对两个属性进行排序(这里还要考虑空值处理):

public class CameraComparer : IComparer<Camera>
{
    private SortDirection typeSortDirection;
    private SortDirection nameSortDirection;

    public CameraComparer(SortDirection typeSortDirection, 
                          SortDirection nameSortDirection)
    {
        this.typeSortDirection = typeSortDirection;
        this.nameSortDirection = nameSortDirection;
    }

    public int Compare(Camera x, Camera y)
    {
        if (x.Type == y.Type)
            return x.Name.CompareTo(y.Name) * 
           (nameSortDirection == SortDirection.Ascending ? 1 : -1);

        return x.Type.CompareTo(y.Type) * 
           (typeSortDirection == SortDirection.Ascending ? 1 : -1);
    }
}

public enum SortDirection
{
    Ascending,
    Descending
}

用法:

myList.Sort(new CameraComparer(SortDirection.Ascending, SortDirection.Descending));
于 2013-03-19T12:08:57.297 回答
1

我将Tuples用于这样的情况:

public int Compare(Camera x, Camera y) {
    var xx = Tuple.Create(x.name, x.type);
    var yy = Tuple.Create(y.name, y.type);
    return xx.CompareTo(yy);
}

也就是说,假设您要以相同的顺序比较所有属性。如果不是,我认为name您可以通过执行以下操作来反转考虑属性(例如)的顺序:

var xx = Tuple.Create(y.name, x.type);
var yy = Tuple.Create(x.name, y.type);

也就是说,把它放在“错误的”元组中。但是我根本没有测试过这部分。

于 2013-03-19T12:04:26.063 回答
1
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;

    if (x.name.CompareTo(y.name) > 0)
        return asc ? -1 : 1;

    if (x.name.CompareTo(y.name) < 0)
        return asc ? 1 : -1;

    return 0;
}
于 2013-03-19T11:56:31.557 回答