##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.
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.
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.
- New class,
LeistungComparer : Comparer<T>
public abstract class Comparer<T> : IComparer<T>
- so inherit and we get the interface.- We'll pass a constructor parameter to tell which way to sort.
- Move
Leistung.CompareTo()
code to this new class. Leistung.CompareTo()
will just call theLeistungComparer.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.
- New class,
LeistungComparer : Comparer<T>
public abstract class Comparer<T> : IComparer<T>
- so inherit and we get the interface.- We'll pass a constructor parameter to tell which way to sort.
- Move
Leistung.CompareTo()
code to this new class. Leistung.CompareTo()
will just call theLeistungComparer.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: