문제

I have a record class like this :

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;
}

and I made a list of RecordInfo like this :

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

and I added records to it, but I would like to sort that list based on Patient_ID, sex, Fname, etc.....

Any idea how can I do that?

도움이 되었습니까?

해결책

Sure, LINQ makes it easy using OrderBy, OrderByDescending, ThenBy and ThenByDescending:

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

That will create a new list - LINQ is based on queries which project one sequence onto a new one, rather than mutating the existing list.

Alternatively, you could use List<T>.Sort with a custom comparer to sort in-place.

Either way, I'd strongly recommend using properties instead of public fields, and also following .NET naming conventions.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top