泛型系列反轉Sorted List裡的內容

來源:互聯網
上載者:User
4.8 反轉Sorted List裡的內容

問題

您希望在數組和清單類型中可以反轉sorted list裡的內容同時又維持SortedList和SortedList<T>類原來的功能。無論是SortedList還是泛型SortedList<T>類都直接提供了完成這個功能的方法而又不需要重填列表。

解決方案

ReversibleSortedList<TKey, TValue>類提供了這些功能,它基於SortedList<TKey, TValue>類,所以擁有相同的功能,它提供了額外的功能是很容易反轉已排序的列表。

在執行個體化ReversibleSortedList<TKey, TValue>之後,鍵是整數類型,值是字串類型,一連串無序的數字和它們的文本表達被插入到列表中。這些項目是這樣顯示的:

ReversibleSortedList<int, string> rsl = new ReversibleSortedList<int, string>();

rsl.Add(2, "2");

rsl.Add(5, "5");

rsl.Add(3, "3");

rsl.Add(1, "1");

foreach (KeyValuePair<int, string> kvp in rsl)

{

    Debug.WriteLine("\t" + kvp.Key + "\t" + kvp.Value);

}

列表輸出顯示為按升序排序(預設):

    1   1
    2   2
    3   3
    5   5

現在排列順序通過設定ReversibleSortedList的SortDirection屬性被反轉為降序。為了重新排序需要調用Sort()方法。結果如下:

// 轉換排序方向.

rsl.Comparer.SortDirection = ListSortDirection.Descending;

// 重排列表.

rsl.Sort();

foreach (KeyValuePair<int, string> kvp in rsl)

{

    Debug.WriteLine("\t" + kvp.Key + "\t" + kvp.Value);

}

這一次,輸出為降序:

    5   5
    3   3
    2   2

1   1

當把一個新項添加進列表,它將按當前的排列順序被添加進去,但在添加完所有項後馬上進行反轉,就可以保持列表中元素的順序。

    rsl.Add(4, "4");

    foreach (KeyValuePair<int, string> kvp in rsl)

    {

        Debug.WriteLine("\t" + kvp.Key + "\t" + kvp.Value);

    }

    // 轉換排序方向.

    rsl.Comparer.SortDirection = ListSortDirection.Ascending;

    // 重排列表.

    rsl.Sort();

    foreach (KeyValuePair<int, string> kvp in rsl)

    {

        Debug.WriteLine("\t" + kvp.Key + "\t" + kvp.Value);

}

可以看到新項即按降序也按升序排列:

    5   5
    4   4
    3   3
    2   2
    1   1
    1   1
    2   2
    3   3
    4   4
    5   5

 

ReversibleSortedList<TKey, TValue>包含一個實現了IComparer<T>介面的嵌套類SortDirectionComparer<T>。這個類可以在“討論”這一節中的ReversibleSortedList<TKey, TValue>代碼中看到。一個實現了IComparer<T>介面的類可以做為ReversibleSortedList<TKey, TValue>構造方法的參數來改變預設的排序。IComparer<T>介面實現了Compare方法:

class Program

    {

        public int Compare(T lhs, T rhs)

        {

            int compareResult =

                lhs.ToString().CompareTo(rhs.ToString());

            // 如果為降序, 則反轉

            if (SortDirection == ListSortDirection.Descending)

                compareResult *= -1;

            return compareResult;

        }

}

Compare方法使用了SortDirectionComparer<T>類的SortDirection屬性來決定項的排序。這個屬性在ReversibleSortedList<TKey, TValue>的內部類SortDirectionComparer<T>執行個體中被設定。SortDirection屬性是在構造方法中被設定的,代碼如下:

    public ReversibleSortedList()

    {

        this.keys = ReversibleSortedList<TKey, TValue>.emptyKeys;

        this.values = ReversibleSortedList<TKey, TValue>.emptyValues;

        this._size = 0;

        this._sortDirectionComparer = new SortDirectionComparer<TKey>();

        this._currentSortDirection = this._sortDirectionComparer.SortDirection;

}

這允許它在指定時間內反轉排列順序,但並沒有重排列表中已存在的項。為了實現這個功能,需要在Reversible-SortedList<TKey, TValue>類中添加一個新的Sort()方法以重排列表。代碼如下:

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;

     }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.