本文介绍了对自定义类列表进行排序<T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 date
属性对我的列表进行排序.
I would like to sort my list with the date
property.
这是我的自定义类:
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; }
}
}
这是我要排序的List
:
List<cTag> Week = new List<cTag>();
我想通过 cTag
类的 date
属性对 List
进行排序.日期格式为 dd.MM.yyyy
.
I want to sort the List
by the date
property of the cTag
class.The date is in the format dd.MM.yyyy
.
我阅读了一些关于 IComparable
接口的内容,但我不知道如何使用它.
I read something about the IComparable
interface, but I don't know how to use it.
推荐答案
一种方法是使用 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); });
这篇关于对自定义类列表进行排序<T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!