Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.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#_List_Sorting - Fatal编程技术网

按属性对对象列表进行排序c#

按属性对对象列表进行排序c#,c#,list,sorting,C#,List,Sorting,我有这门课: public class Leg { public int Day { get; set; } public int Hour { get; set; } public int Min { get; set; } } 我有一个获取腿列表的函数,称为GetLegs() List legs=GetLegs(); 现在我想对这个列表进行排序。所以我首先要考虑一天,然后一小时,最后一分钟。 我应该如何解决这个排序问题 谢谢您可以编写一个自定义的i比较程序并将其传

我有这门课:

public class Leg
{
    public int Day { get; set; }
    public int Hour { get; set; }
    public int Min { get; set; }
}
我有一个获取腿列表的函数,称为GetLegs()

List legs=GetLegs();
现在我想对这个列表进行排序。所以我首先要考虑一天,然后一小时,最后一分钟。 我应该如何解决这个排序问题


谢谢

您可以编写一个自定义的
i比较程序
并将其传递到
列表中。Sort
方法


或者,您可以在类中实现
IComparable
,只需调用
List.Sort
使用方法。

我想这会有所帮助

var o = legs.OrderBy(x => x.Day)
            .ThenBy(x => x.Hour)
            .ThenBy(x => x.Min);

也许是这样的:

List<Leg> legs = GetLegs()
                .OrderBy(o=>o.Day)
                .ThenBy(o=>o.Hour)
                .ThenBy(o=>o.Min).ToList();
List legs=GetLegs()
.OrderBy(o=>o.Day)
.然后(o=>o.Hour)
.ThenBy(o=>o.Min).ToList();

您需要在类上实现
IComparable
接口,以便以更直观的方式对对象进行C#语言排序。当类实现
IComparable
时,还必须实现
public方法
CompareTo(T)。

Leg
类实现了
IComparable
,这意味着
Leg
实例可以与其他
Leg
实例进行比较

    #region "Leg Class that implements IComparable interface"
    public class Leg:IComparable<Leg>
    {
        public int Day { get; set; }
        public int Hour { get; set; }
        public int Min { get; set; }

        public int CompareTo(Leg leg)
        {
            if (this.Day == leg.Day)
            {
                if (this.Hour == leg.Hour)
                {
                    return this.Min.CompareTo(leg.Min);
                }
            }
            return this.Day.CompareTo(leg.Day);
        }
    }
    #endregion


   //Main code
   List<Leg> legs = GetLegs();
   legs.Sort();
#区域“实现IComparable接口的Leg类”
公共类腿:IComparable
{
公共整数日{get;set;}
公共整数小时{get;set;}
公共int Min{get;set;}
公共内部比较(腿部)
{
如果(this.Day==leg.Day)
{
如果(this.Hour==leg.Hour)
{
将此.Min.compare返回到(leg.Min);
}
}
返回this.Day.CompareTo(leg.Day);
}
}
#端区
//主代码
List legs=GetLegs();
legs.Sort();

为什么不使用本机
DateTime
struct?支持开箱即用的比较和排序。顺便说一句,看看右边的相关问题,有很多类似的线索,你需要学习搜索一下。举个例子(我看的第一个)。。。如你所见,这是我在这里的第一篇帖子,所以我可能会更习惯于事情的运作方式;)无论如何,感谢您的回复,不幸的是,我不能在此场景中使用DateTime。即使不需要这样做,他也可以在
列表中编写匿名lambda。Sort
。出于代码重用的原因,如果排序/比较发生在多个位置,则实现
IComparable
可能是更好的选择。
    #region "Leg Class that implements IComparable interface"
    public class Leg:IComparable<Leg>
    {
        public int Day { get; set; }
        public int Hour { get; set; }
        public int Min { get; set; }

        public int CompareTo(Leg leg)
        {
            if (this.Day == leg.Day)
            {
                if (this.Hour == leg.Hour)
                {
                    return this.Min.CompareTo(leg.Min);
                }
            }
            return this.Day.CompareTo(leg.Day);
        }
    }
    #endregion


   //Main code
   List<Leg> legs = GetLegs();
   legs.Sort();