9. Perfect
The building has been built, the rest of the work is decorated, the decoration is ready to stay. From the topic of this article, this is a sort of inverted collection class, but we only implement the descending insertion function, what if we want to convert ascending to descending order? The workaround for this example is to declare a property comparer that represents the sort direction and to add a sort method that is sorted according to the comparer property when the sort method is called:
private ListSortDirection _currentSortDirection = ListSortDirection.Descending;
public SortDirectionComparer<TKey> Comparer
{
get
{
return this._sortDirectionComparer;
}
}
public void Sort()
{
// 检查是否跟现有排序方向相同.
if (this._currentSortDirection != this._sortDirectionComparer.SortDirection)
{
// 如果不同,则进行反转.
Array.Reverse (this.keys, 0, this._size);
Array.Reverse (this.values, 0, this._size);
// 设置当前排序.
this._currentSortDirection = this._sortDirectionComparer.SortDirection;
}
}
Where the Sortdirectioncomparer class is the class declared in section II, refer to:
http://cgbluesky.blog.163.com/blog/static/241235582008113103320 661/
Next, add a feature that cuts out the extra space:
//剪除多 余空间
public void TrimExcess()
{
int num1 = (int)(this.keys.Length * 0.9);
if (this._size < num1)
{
this.Capacity = this._size;
}
}
Of course, you need to add a set method to the Capacity property
public int Capacity //容量属性
{
get
{
return this.keys.Length;
}
set
{
this.InternalSetCapacity(value, true);
}
}