I have inherited this web app; that stores alot of info in collections; And it sorts it's collections using generics (I think).
Well now I need to sort by multiple fields and I don't know how to do this with my generic functions.
I need to sort the collection by price, then by day's then by name all asc. The following is the collection class that has the sort functionality.
Any help would be welcome.
Thanks,
public static bool operator <(RateQuote r1, RateQuote r2)
{
Decimal d1 = 0, d2 = 0;
String s1 = (r1 != null && r1.Actual_Price != null) ?
r1.Actual_Price.Replace("$", "") :
String.Empty;
String s2 = (r2 != null && r2.Actual_Price != null) ?
r2.Actual_Price.Replace("$", "") :
String.Empty;
Decimal.TryParse(s1, out d1);
Decimal.TryParse(s2, out d2);
return d1 < d2;
}
public static bool operator <=(RateQuote r1, RateQuote r2)
{
return (!(r1 > r2));
}
public static bool operator >(RateQuote r1, RateQuote r2)
{
Decimal d1 = 0, d2 = 0;
String s1 = (r1 != null && r1.Actual_Price != null) ?
r1.Actual_Price.Replace("$", "") :
String.Empty;
String s2 = (r2 != null && r2.Actual_Price != null) ?
r2.Actual_Price.Replace("$", "") :
String.Empty;
Decimal.TryParse(s1, out d1);
Decimal.TryParse(s2, out d2);
return d1 > d2;
}
public static bool operator >=(RateQuote r1, RateQuote r2)
{
return (!(r1 < r2));
}
#endregion
#region IComparable
/// <summary>
/// Implements Systems.Collections.Generic.IComparable<Rate>.
/// </summary>
/// <param name="rate2">
/// The Rate against which this Rate will be compared
/// </param>
/// <returns>
/// -1 if this Rate is the lesser.
/// 1 if this Rate is the greater.
/// else 0.
/// </returns>
public int CompareTo(RateQuote rate2)
{
return ((this < rate2 ) ? -1 : (this > rate2) ? 1 : 0);
}
#endregion
}
/// <summary>
/// Defines a collection of Rate objects.
/// </summary>
public class RateQuoteCollection : List<RateQuote>
{
}
Ordinary Programmer
Well now I need to sort by multiple fields and I don't know how to do this with my generic functions.
I need to sort the collection by price, then by day's then by name all asc. The following is the collection class that has the sort functionality.
Any help would be welcome.
Thanks,
public static bool operator <(RateQuote r1, RateQuote r2)
{
Decimal d1 = 0, d2 = 0;
String s1 = (r1 != null && r1.Actual_Price != null) ?
r1.Actual_Price.Replace("$", "") :
String.Empty;
String s2 = (r2 != null && r2.Actual_Price != null) ?
r2.Actual_Price.Replace("$", "") :
String.Empty;
Decimal.TryParse(s1, out d1);
Decimal.TryParse(s2, out d2);
return d1 < d2;
}
public static bool operator <=(RateQuote r1, RateQuote r2)
{
return (!(r1 > r2));
}
public static bool operator >(RateQuote r1, RateQuote r2)
{
Decimal d1 = 0, d2 = 0;
String s1 = (r1 != null && r1.Actual_Price != null) ?
r1.Actual_Price.Replace("$", "") :
String.Empty;
String s2 = (r2 != null && r2.Actual_Price != null) ?
r2.Actual_Price.Replace("$", "") :
String.Empty;
Decimal.TryParse(s1, out d1);
Decimal.TryParse(s2, out d2);
return d1 > d2;
}
public static bool operator >=(RateQuote r1, RateQuote r2)
{
return (!(r1 < r2));
}
#endregion
#region IComparable
/// <summary>
/// Implements Systems.Collections.Generic.IComparable<Rate>.
/// </summary>
/// <param name="rate2">
/// The Rate against which this Rate will be compared
/// </param>
/// <returns>
/// -1 if this Rate is the lesser.
/// 1 if this Rate is the greater.
/// else 0.
/// </returns>
public int CompareTo(RateQuote rate2)
{
return ((this < rate2 ) ? -1 : (this > rate2) ? 1 : 0);
}
#endregion
}
/// <summary>
/// Defines a collection of Rate objects.
/// </summary>
public class RateQuoteCollection : List<RateQuote>
{
}
Ordinary Programmer