Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

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

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

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

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

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

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

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.

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.

added 3284 characters in body
Source Link
radarbob
  • 8.2k
  • 21
  • 35

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:

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

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:

Source Link
radarbob
  • 8.2k
  • 21
  • 35
Loading
lang-cs

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