简体   繁体   中英

Sort multi-level list in C# where the inner layer contains a list of typed objects

I have a List containing multiple Lists that contain typed objects themselves:

List<List<Activity>> SortActivities

An object of type Activity looks like this:

public class Activity
{
    public DateTime Date { get; set; }
    public string ProjectnumberUnformatted { get; set; }
    public int Year 
    { 
        get
        {
            return Date.Year;
        }
    }
    public int Month
    { 
        get
        {
            return Date.Month;
        }
    }
    public string MonthYear 
    { 
        get
        {
            return Month + " / " + Year;
        }
    }
    public string Customer 
    {
        get 
        {
            return ProjectnumberUnformatted.Split('/')[0];            
        } 
    }
    public string ProjectnumerFormatted 
    { 
        get
        {
            return ProjectnumberUnformatted.Split('/')[2];
        }      
    }
    public string MonthString 
    {
        get
        {
            switch (Month)
            {
                case 1:
                        return "Januar";
                case 2:
                        return "Februar";
                case 3:
                        return "März";
                case 4:
                        return "April";
                case 5:
                        return "Mai";
                case 6:
                        return "Juni";
                case 7:
                        return "Juli";
                case 8:
                        return "August";
                case 9:
                        return "September";
                case 10:
                        return "Oktober";
                case 11:
                        return "November";
                case 12:
                        return "Dezember";
                default:
                        return "Invalid";
            }
        }
    }
    public string Start { get; set; }
    public string Pause { get; set; }
    public string End { get; set; }
    public string Description { get; set; }
    public string Comment { get; set; }
}

At program start, I have an object List<Activity> that contains unsorted activities. However, each activity should be sorted by it's project number. So I need to implement a method that sorts each activity from the List<Activity> object into it's own List<Activity> with the project number as the sorting argument. Then the resulting Lists are put into List<List<Activity>> .

I just need a basic flowchart if possible with a simple algorithm that does this job as I honestly have no idea how to start.

It sounds like you actually want a dictionary, where the key is the project number and the value is a list of activities. Sounds like an ideal job for LINQ:

List<Activity> activities = new List<Activity>
{
    new Activity { ProjectnumberUnformatted = "abc" },
    new Activity { ProjectnumberUnformatted = "def" },
    new Activity { ProjectnumberUnformatted = "abc" },
    new Activity { ProjectnumberUnformatted = "abc" },
    new Activity { ProjectnumberUnformatted = "def" },
    new Activity { ProjectnumberUnformatted = "ghi" },
    new Activity { ProjectnumberUnformatted = "def" },
};

Dictionary<string, List<Activity>> activitiesKeyedOnProjectNumber = (
    from activity in activities
    group activity by activity.ProjectnumberUnformatted into grouped
    select new { key = grouped.Key, value = grouped.ToList() }
    ).ToDictionary(
        keySelector: x => x.key,
        elementSelector: x => x.value
        );

You do not want to sort objects, you want to group them. What you need to do is to pass trough all elements in collection and assign each of them to group with correct identifier. Dictionary<key,List<object>> would be better choice for data structure in such case.

Dictionary<string, List<ISortableObject>> Sort(List<ISortableObject> items)
{
  var result = new Dictionary<string, List<ISortableObject>>();
  foreach(var item in items)
  {
    if (!result.ContainsKey(item.SortingKey))
    { result[item.SortingKey]=new List<ISortableObject>(); }
    result[item.SortingKey].Add(item);
  }
}

OK thanks for all the answers. I looked into LINQ a little and came up with a really simple statement that served my purpose:

var groupedActivities = activities.GroupBy(p => p.ProjectnumberUnformatted);

with this, I get an enumeration containing other enumerations of typed Activity objects and can then iterate through those sub-enumerations and do what I need to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM