我想用date物业排序我的清单.
这是我的自定义类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Test.Web
{
public class cTag
{
public int id { get; set; }
public int regnumber { get; set; }
public string date { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
这是List我要排序的:
List<cTag> Week = new List<cTag>();
Run Code Online (Sandbox Code Playgroud)
我想要做的是按照datecTag类的属性对List进行排序.日期格式为:dd.MM.yyyy.
我读了一些关于IComparable界面的东西,但不知道如何使用它.
ahs*_*ele 137
一种方法是使用a delegate
List<cTag> week = new List<cTag>();
// add some stuff to the list
// now sort
week.Sort(delegate(cTag c1, cTag c2) { return c1.date.CompareTo(c2.date); });
Run Code Online (Sandbox Code Playgroud)
Ond*_* C. 48
你的cTag类必须实现IComparable<T>接口是正确的.然后你就可以打电话Sort()给你的清单.
要实现IComparable<T>接口,必须实现CompareTo(T other)方法.最简单的方法是调用要比较的字段的CompareTo方法,在您的情况下是日期.
public class cTag:IComparable<cTag> {
public int id { get; set; }
public int regnumber { get; set; }
public string date { get; set; }
public int CompareTo(cTag other) {
return date.CompareTo(other.date);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这不太合适,因为这会对字符串使用经典排序(因为您将日期声明为字符串).所以我认为最好的想法是重新定义类并将日期声明为字符串,而不是DateTime.代码几乎保持不变:
public class cTag:IComparable<cTag> {
public int id { get; set; }
public int regnumber { get; set; }
public DateTime date { get; set; }
public int CompareTo(cTag other) {
return date.CompareTo(other.date);
}
}
Run Code Online (Sandbox Code Playgroud)
只有在创建类的实例时才需要做的事情是将包含日期的字符串转换为DateTime类型,但是可以通过DateTime.Parse(String)方法轻松完成.
Yak*_*ych 36
对于这种情况,您还可以使用LINQ进行排序:
week = week.OrderBy(w => DateTime.Parse(w.date)).ToList();
Run Code Online (Sandbox Code Playgroud)
Dar*_*rov 12
List<cTag> week = new List<cTag>();
week.Sort((x, y) =>
DateTime.ParseExact(x.date, "dd.MM.yyyy", CultureInfo.InvariantCulture).CompareTo(
DateTime.ParseExact(y.date, "dd.MM.yyyy", CultureInfo.InvariantCulture))
);
Run Code Online (Sandbox Code Playgroud)
首先,如果date属性存储日期,则使用DateTime存储它.如果你通过排序解析日期,你必须为每个被比较的项目解析它,这不是很有效......
然后你可以做一个IComparer:
public class TagComparer : IComparer<cTag>
{
public int Compare(cTag first, cTag second)
{
if (first != null && second != null)
{
// We can compare both properties.
return first.date.CompareTo(second.date);
}
if (first == null && second == null)
{
// We can't compare any properties, so they are essentially equal.
return 0;
}
if (first != null)
{
// Only the first instance is not null, so prefer that.
return -1;
}
// Only the second instance is not null, so prefer that.
return 1;
}
}
var list = new List<cTag>();
// populate list.
list.Sort(new TagComparer());
Run Code Online (Sandbox Code Playgroud)
你甚至可以作为代表来做:
list.Sort((first, second) =>
{
if (first != null && second != null)
return first.date.CompareTo(second.date);
if (first == null && second == null)
return 0;
if (first != null)
return -1;
return 1;
});
Run Code Online (Sandbox Code Playgroud)
你是对的 - 你需要实现IComparable.要做到这一点,只需声明您的类:
public MyClass : IComparable
{
int IComparable.CompareTo(object obj)
{
}
}
Run Code Online (Sandbox Code Playgroud)
在CompareTo中,您只需实现自定义比较算法(您可以使用DateTime对象来执行此操作,但只需确保首先检查"obj"的类型).有关详细信息,请参阅此处和此处.
你可以使用linq:
var q = from tag in Week orderby Convert.ToDateTime(tag.date) select tag;
List<cTag> Sorted = q.ToList()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
120596 次 |
| 最近记录: |