Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的排序表#_C#_List_C# 4.0_Collections_Arraylist - Fatal编程技术网

C# C语言中的排序表#

C# C语言中的排序表#,c#,list,c#-4.0,collections,arraylist,C#,List,C# 4.0,Collections,Arraylist,我有一个自定义类型的列表。自定义类型为 public class PossibleMatch { public PossibleMatch() { StoreIds = new List<long>(); OrderLineIds = new List<long>(); } public IList<long> StoreIds { get;

我有一个自定义类型的列表。自定义类型为

public class PossibleMatch
    {
        public PossibleMatch()
        {
            StoreIds = new List<long>();
            OrderLineIds = new List<long>();
        }
        public IList<long> StoreIds { get; set; }
        public IList<long> OrderLineIds { get; set; }
    }
公共类可能匹配
{
公共可能匹配()
{
storeid=新列表();
OrderLineIds=新列表();
}
公共IList存储ID{get;set;}
公共IList OrderlineId{get;set;}
}
我必须以这样一种方式对列表进行排序,即商店数量较少且订单行数量较多的项目应位于顶部


提前感谢。

LINQ提供了一些方法

请尝试以下操作,首先按具有最少StoreID的匹配项进行订购,然后再按具有最多OrderLineID的匹配项进行子订购:

 var possibleMatches = new List<PossibleMatch>();
 var ordered = possibleMatches.OrderBy(pm => pm.StoreIds.Count).ThenByDesc(pm => pm.OrderLineIds.Count);
生成自定义比较器:

public class PossibleMatchComparer: IComparer<PossibleMatch>
{
    public int Compare(PossibleMatch x, PossibleMatch y)
    {
        if (x.StoreIds.Count < y.StoreIds.Count) return -1;
        if (x.StoreIds.Count > y.StoreIds.Count) return 1;

        return y.OrderLineIds.Count - x.OrderLineIds.Count;
    }
}

谢谢回复@Cuong。自定义比较器与LINQ的不同之处OrderBy@Naresh它对列表进行排序,而不是返回一个
IEnumerable
。谢谢你,我还没有试过。将尝试并更新。
public class PossibleMatchComparer: IComparer<PossibleMatch>
{
    public int Compare(PossibleMatch x, PossibleMatch y)
    {
        if (x.StoreIds.Count < y.StoreIds.Count) return -1;
        if (x.StoreIds.Count > y.StoreIds.Count) return 1;

        return y.OrderLineIds.Count - x.OrderLineIds.Count;
    }
}
list.Sort(new PossibleMatchComparer());