
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
244
|
Chapter 4: Generics
Discussion
ReversibleSortedList<TKey,TValue> is shown in its entirety in Example 4-3.
Example 4-3. ReversibleSortedList class
[Serializable, ComVisible(false), DebuggerDisplay("Count = {Count}")]
public class ReversibleSortedList<TKey, TValue> :
IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>,
IEnumerable<KeyValuePair<TKey, TValue>>,
IDictionary, ICollection, IEnumerable
{
#region SortDirectionComparer class definition
public class SortDirectionComparer<T> : IComparer<T>
{
private System.ComponentModel.ListSortDirection _sortDir;
public SortDirectionComparer( )
{
_sortDir = ListSortDirection.Ascending;
}
public SortDirectionComparer(ListSortDirection sortDir)
{
_sortDir = sortDir;
}
public System.ComponentModel.ListSortDirection SortDirection
{
get { return _sortDir; }
set { _sortDir = value; }
}
public int Compare(T lhs, T rhs)
{
int compareResult =
lhs.ToString( ).CompareTo(rhs.ToString( ));
// If order is DESC, reverse this comparison.
if (SortDirection == ListSortDirection.Descending)
compareResult *= -1;
return compareResult;
}
}
#endregion // SortDirectionComparer
#region CTORS
static ReversibleSortedList( )
{
ReversibleSortedList<TKey, TValue>.emptyKeys = new TKey[0];
ReversibleSortedList<TKey, TValue>.emptyValues = new TValue[0]; ...