Build a reversible sort of generic dictionary class (9 end)--perfect

Source: Internet
Author: User
Tags sort

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);
    }
  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.