2012-08-22 55 views
0

我有一个记录类这样的:列表排序

public class RecordInfo 
{ 
    public String CDate; 
    public String Patient_ID; 
    public Color Zone; 
    public String Fname; 
    public String Lname; 
    public Int64 ImgSize; 
    public String ImagePrefix; 
    public String ImagePath; 
    public String Sex; 
    public String TZ; 
} 

,我犯了这样的RecordInfo的列表:

List<RecordInfo> PatientRecords = new List<RecordInfo>(); 

,我添加的记录它,但我会喜欢根据Patient_ID,性别,Fname等对该列表进行排序.....

任何想法我该怎么做?

回答

3

当然,LINQ可以轻松使用OrderByOrderByDescendingThenByThenByDescending

// Note change from Patient_ID to PatientId for naming conventions. 
List<RecordInfo> sorted = records.OrderBy(x => x.PatientId).ToList(); 

这将创建一个列表 - LINQ是基于哪个项目一个序列到一个新的查询,而比改变现有的列表。

或者,您可以使用List<T>.Sort与自定义比较器就地排序。

无论哪种方式,我强烈建议使用属性,而不是公共字段,也遵循.NET命名约定。

+0

记录是指什么? 因为我试图用PatientRecords替换它,但它没有工作 –

+1

@ R.Vector,“它没有工作”?你能用英文翻译“它没有工作”吗?从软件开发人员的角度来看,不是软件用户。 –

+0

是的它说:名称'记录'在当前上下文中不存在 –