1

I have a data structure as follows:

public class BranchLevel_1
{
    public string Name { get; set; }
    public ObservableCollection<BranchLevel_2> Children { get; set; }

    public BranchLevel_1(string name, List<BranchLevel_2> children)
    {
        this.Name = name;
        this.Children = new ObservableCollection<BranchLevel_2>(children);
    }
}

public class BranchLevel_2
{
    public ObservableCollection<BranchLevel_3> Contents { get; set; }

    public BranchLevel_2(List<string> contents)
    {
        this.Contents = new ObservableCollection<BranchLevel_3>();
        for (int i = 0; i < contents.Count; i++)
        {
            this.Contents.Add(new BranchLevel_3(contents[i]));
        }
    }
}

public class BranchLevel_3
{
    public string Content { get; set; }

    public BranchLevel_3(string text)
    {
        this.Content = text;
    }
}

Sorting data on the first level is easy and I can obtain in easily by:

Level1_Data.OrderBy(item => item.Name).ToList()

However, I am stuck with sorting on the second level. BranchLevel_2 class is just a container for items stored in BranchLevel_3 classes. Therefore I would like to sort Level2 with data stored in BranchLevel_2.Contents1.Content value. This syntax for me seems to be correct and I cannot locate the problem...

  Level1_Data.Select(item_Level1 => item_Level1.Children.OrderBy(item_Level2 => item_Level2.Contents[1].Content)).ToList();  

Any hints?

Here is the rusult (indicated in yellow is supposed to be sorted alphabetically) enter image description here

Luki
  • 39
  • 5
  • Is there an exception, or it's just a not expected result? Have you tried 'Branch_Level1.SelectMany(...' instead? – Renat Apr 29 '19 at 13:49
  • no exception at all. The data is just not sorted as expected – Luki Apr 29 '19 at 13:54
  • Are you attempting to sort the sub collections or by a value in the subcollection at a specific index? I am not sure what you are looking to do but if you want the subcollections sorted, just sort them when you load them. – hawkstrider Apr 29 '19 at 13:54
  • I am sorting subcollections. These cannot be sorted/compared, that why I am trying to access a specific value in 'subcollection collection' – Luki Apr 29 '19 at 13:57
  • 1
    Please give us some sample data and what order you are actually trying to achieve with it... – GPW Apr 29 '19 at 13:58
  • Something to note here is that you are using ObservableCollections and sorting of them will create an IEnumerable and will not sort the underlying ObservableCollection. If you need to truly sort and ObservableCollection, you will need to recreate it after the sort. Please see [Sort ObservableCollection through C#](https://stackoverflow.com/questions/19112922/sort-observablecollectionstring-through-c-sharp) – hawkstrider Apr 29 '19 at 14:17
  • Perfect that solved the problem! I sorted the list before creating new ObservableCollection – Luki Apr 29 '19 at 14:43

2 Answers2

0

Why not just sort the contents before adding them to the ObservableCollection

public class BranchLevel_2
{
    public ObservableCollection<BranchLevel_3> Contents { get; set; }

    public BranchLevel_2(List<string> contents)
    {
        this.Contents = new ObservableCollection<BranchLevel_3>();
        foreach (var content in contents.OrderBy(c => c))
        {
            this.Contents.Add(new BranchLevel_3(content));
        }
    }
}
hawkstrider
  • 4,141
  • 16
  • 27
0

Here is a solution that solved the problem, thanks to suggestion from @bhmahler

public class BranchLevel_1
{
    public string Name { get; set; }
    public ObservableCollection<BranchLevel_2> Children { get; set; }

    public BranchLevel_1(string name, List<BranchLevel_2> children)
    {
        this.Name = name;
        //this line needs to be added before creating ObservableCollection
        children.OrderBy(item_level2 => item_level2.Contents[1].Content).ToList();
        this.Children = new ObservableCollection<BranchLevel_2>(children);
    }
}
Luki
  • 39
  • 5