如何合并两个排序功能?

时间:2013-03-19 11:50:51

标签: c# list sorting

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

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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);

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

答案 2 :(得分: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));