IList with Implicit Sort Order

I would like to create IList<Child>one that always saves its objects Childin the default / implicit sort order (i.e. regardless of the additions / paragraphs to the base list).

What I'm specifically trying to avoid is the need for all consumers of the above to IList<Child>explicitly call IEnumerable<T>.OrderBy()each time they want to list it. Besides violating DRY, this approach will also break encapsulation, as consumers should know that my list is even sorted, which is actually not their business :)

The solution that seemed most logical / effective was to show IList<Child>how IEnumerable<Child>(to prevent List mutations) and add explicit add / remove methods to the containing one Parent. That way, I can intercept changes in the list that require re-sorting, and apply one through Linq:

public class Child {
  public string StringProperty;
  public int IntProperty;
}

public class Parent{
private IList<Child> _children = new List<Child>();

      public IEnumerable<Child> Children{
      get
         {

            return _children;
         }
      }
      private void ReSortChildren(){
        _children = new List<Child>(child.OrderBy(c=>c.StringProperty));
      }
      public void AddChild(Child c){
          _children.Add();
          ReSortChildren()
      }
      public void RemoveChild(Child c){
          _children.Remove(c);
          ReSortChildren()
      }
}

However, this approach does not capture the changes made to the base one Child.StringProperty(which in this case is the property that controls sorting). There should be a more elegant solution to such a basic problem, but I could not find it.

EDIT: I was unclear that I would have preferred a LINQ compatible solution. I would prefer not to resort to using .NET 2.0 constructs (i.e. SortedList)

+3
3

: Child OnStringPropertyChanged, StringProperty. SortedList, Add, . , , StringProperty. Child, -, , Child .

, SortedList, , StringProperty. , StringProperty , , .

, Child, StringProperty .

public class Parent{
  private SortedList<string, Child> _children = new SortedList<string, Child>();

  public ReadOnlyCollection<Child> Children{
    get { return new ReadOnlyCollection<Child>(_children.Values); }
  }

  public void AddChild(string stringProperty, int data, Salamandar sal){
    _children.Add(stringProperty, new Child(stringProperty, data, sal));
  }

  public void RemoveChild(string stringProperty){
    _children.Remove(stringProperty);
  }

  private void UpdateChildStringProperty(Child c, string newStringProperty) {
    if (c == null) throw new ArgumentNullException("c");

    RemoveChild(c);
    c.StringProperty = newStringProperty;
    AddChild(c);
  }

  public void CheckSalamandar(string s) {
    if (_children.ContainsKey(s))
      var c = _children[s];
      if (c.Salamandar.IsActive) {
        // update StringProperty through our method
        UpdateChildStringProperty(c, c.StringProperty.Reverse());
        // update other properties directly
        c.Number++;
    }
  }
}
0

SortedList<>?

+1

, KeyedCollection, , . .

EDIT:

If this works, unfortunately, it will not be easy. Neither the main search vocabulary nor the basic list in this guy are sorted, and they are not exposed to enough impact so that you can replace them. However, this can serve as an example for your implementation.

0
source

Source: https://habr.com/ru/post/1735641/


All Articles