Skip to main content
Code Review

Return to Revisions

4 of 4
Commonmark migration

Edit

My original answer does not address ascending / descending sorting dynamically. So here is my idea. Note that this is different from @ratchetFreak answer. The key is that an IComparer<T> automatically overrides an object's IComparable<T> implementation.

  1. New class, LeistungComparer : Comparer<T>
    1. public abstract class Comparer<T> : IComparer<T> - so inherit and we get the interface.
    2. We'll pass a constructor parameter to tell which way to sort.
  2. Move Leistung.CompareTo() code to this new class.
  3. Leistung.CompareTo() will just call the LeistungComparer.Compare() - that's all!

The above in action:

// ***** default ascending sort
Leistung yourLeistung = new Leistung( );
Leistung yourLeistung2 = new Leistung( );
List<Leistung> yourList = new List<Leistung>();
yourList.Add(yourLeistung);
yourList.Add(yourLeistung2);
// stuff happens, then...
yourList.Sort();
// override
yourList.Sort( new LeistungComparer( SortOrder.descend ) );

Details

public enum SortOrder { undefined, ascend, descend }
public class LeistungComparer : Comparer<Leistung>
{
 protected int SortBy { get; set; }
 /// <summary>
 /// Default sort order is ascending
 /// </summary>
 /// <param name="sortOrder">defaults to ascend</param>
 public LeistungComparer( SortOrder sortOrder = SortOrder.ascend )
 {
 if ( sortOrder == SortOrder.ascend ) SortBy = 1;
 if ( sortOrder == SortOrder.descend ) SortBy = -1;
 if ( sortOrder == SortOrder.undefined )
 throw new NotImplementedException( "Sort Order is undefined" );
 }
 public override int Compare( Leistung x, Leistung y )
 {
 int result = SortBy;
 if ( x != null && y == null ) return result;
 if ( x == null && y != null ) return result * SortBy;
 if ( x == null && y == null ) return 0;
 result = x.Art.CompareTo( y.Art ) * SortBy;
 if ( result == 0 )
 result = CompareAngebot( x, y );
 return result * SortBy;
 }
 protected int CompareAngebot( Leistung x, Leistung y )
 {
 int result = SortBy;
 result = x.Angebot.CompareTo( y.Angebot ) * SortBy;
 if ( result == 0 )
 result = CompareJahr( x, y );
 return result;
 }
 protected int CompareJahr( Leistung x, Leistung y )
 {
 // you get the idea
 return 1;
 }
}
public class Leistung : IComparable<Leistung>
{
 // properties removed for readability
 protected LeistungComparer Comparer { get; set; }
 public Leistung (LeistungComparer comparer = null){
 Comparer = comparer ?? new LeistungComparer();
 }
 public int CompareTo( Leistung other )
 {
 return Comparer.Compare( this, other );
 }
}

End Edit

NOTE: Original answer follows. It has not been modified.



The idomatic way to enable sorting is to implement the IComparable interface. And this will definitely simplify the sorting code. So:

public class Leistungen : IComparable<Leistungen> { }
public class Aufzeichnung : IComparable<Aufzeichnung> { }

then you can sort like this:

List<Leistungen> myLeistungen; // pretend we instantiated it too.
myLeistungen.Sort();

And if you want different sort algorithms then create an IComparer class for each one as suggested by @ratchetfreak. then you can do the following - which will override your IComparalble implementation in the class Leistungen:

myLeistungen.Sort(myDifferentComparer);

IComparable Implementation

This demonstrates how to compare multiple properties. Both of your classes will implement using this pattern. When Leistungen.CompareTo() gets down to comparing its List<Aufzeichnung>, well Aufzeichnung.CompareTo() takes care of that!

public class Leistungen : IComparable<Leistungen> {
 \\ implementing the generic version means we don't 
 \\ check for or cast to the correct type.
 public int CompareTo(Leistungen other) {
 int result = 1; // "this" is > "other"
 if(other == null) return result;
 result = this.Art.CompareTo(other.Art);
 if(result == 0)
 result = CompareAngebot(other);
 return result;
 }
 protected int CompareAngebot(Leistungen other) {
 int result = 1;
 result = this.Angebot.CompareTo(other.Angebot);
 if(result == 0)
 result = CompareJahr(other);
 return result;
 }
 protected int CompareJahr(Leistungen other) { // you get the idea }
 // ....
 protected int CompareAufzeichnungList(Leistungen other) {
 // last property in our compare chain, so it's real simple
 return this.Aufzeichnungen.CompareTo(other.Aufzeichnungen);
 }
}
radarbob
  • 8.2k
  • 21
  • 35
default

AltStyle によって変換されたページ (->オリジナル) /