leora
leora

Reputation: 196499

In C#, how can I sort a collection of objects by multiple fields of that object?

I have a list of project objects and I want to sort the collection by using a few different fields in the project object.

IEnumerable<Project> list = GetProjectList();

public class Project
{
      public DateTime? Date;
      public string ImportanceLevel;
}  

I first want to

I appreciate this would be better if these were integer values but unfortunately they are stored as strings right now).

Upvotes: 1

Views: 713

Answers (4)

kcwu
kcwu

Reputation: 227

    public class Project    
    {
        public DateTime? Date;
        public string ImportanceLevel;
    }

    var sortedProejcts =
            projects.OrderByDescending(p => p.Date.HasValue)
            .ThenBy(p => Math.Abs(DateTime.Now.CompareTo(p.Date ?? DateTime.MaxValue)))
            .ThenByDescending(
                p =>
                {
                    switch (p.ImportanceLevel)
                    {
                        case "Low":
                            return 1;
                        case "Medium":
                            return 2;
                        case "High":
                            return 3;
                        default:
                            return 0;
                    }
                });

Upvotes: 0

Arghya C
Arghya C

Reputation: 10068

One option is

var sorted = list.OrderBy(l => l.Date.HasValue ? l.Date : DateTime.MaxValue)
    .ThenBy(l => l.ImportanceLevel == "High" ? 1 : 
                    (l.ImportanceLevel == "Medium" ? 2 : 3));

Here, it will do what you want, also it'll sort the projects with same date, by importance.

Or,

var sorted2 = list.OrderBy(l => l.Date.HasValue ? 
                  int.Parse(l.Date.Value.ToString("yyyyMMdd")) :
                  (l.ImportanceLevel == "High" ? 
                      100000001 :
                      (l.ImportanceLevel == "Medium" ? 100000002 : 100000003)));

Where, it'll not sort the projects which have date, by importance.

Upvotes: 3

Shyju
Shyju

Reputation: 218732

//Order by the projects with Date
var projectsWithDate = list.Where(d => d.Date != null)
           .OrderBy(s => s.Date).ToList();

// Projects without Dates

var withoutdate = list.Where(d => d.Date == null)
    .OrderBy(g =>
    {
        if (g.ImportanceLevel == "High")
            return 1;
        if (g.ImportanceLevel == "Medium")
            return 2;
        if (g.ImportanceLevel == "Low")
            return 3;

        return 0;
    })
    .ToList();

//Add the second list to first.
projectsWithDate.AddRange(withoutdate);

// Now you can use projectsWithDate collection.

Upvotes: 1

J3soon
J3soon

Reputation: 3143

This will work

var orderedList = list
                .OrderBy(p => p.Date)
                .ThenByDescending(p =>
                {
                    if (p.ImportanceLevel == "Low")
                        return 1;
                    if (p.ImportanceLevel == "Medium")
                        return 2;
                    if (p.ImportanceLevel == "Hight")
                        return 3;
                    return 0;
                });

Upvotes: 0

Related Questions