2010-01-16 114 views
2

我有一個List<ListViewItem>,我用它在一個虛擬模式ListView中
列表中有5000個項目,並且我會將它與LINQ和OrderBy進行排序,只要ListView的列被點擊,所以我寫了代碼如下:
List <> OrderBy IComparer?


List<ListViewItem> ListOfItems = new List<ListViewItem>(); 
ListViewColumnSorter lvwColumnSorter; 

void listView1_ColumnClick(object sender, ColumnClickEventArgs e) 
{ 
    try 
    { 
     // Determine if clicked column is already the column that is being sorted. 
     if (e.Column == lvwColumnSorter.SortColumn) 
     { 
      // Reverse the current sort direction for this column. 
      if (lvwColumnSorter.Order == SortOrder.Ascending) 
      { 
       lvwColumnSorter.Order = SortOrder.Descending; 
      } 
      else 
      { 
       lvwColumnSorter.Order = SortOrder.Ascending; 
      } 
     } 
     else 
     { 
      // Set the column number that is to be sorted; default to ascending. 
      lvwColumnSorter.SortColumn = e.Column; 
      lvwColumnSorter.Order = SortOrder.Ascending; 
     } 

     if (lvwColumnSorter.Order == SortOrder.Descending) 
      this.ListOfItems.OrderByDescending(a => a, new CompareListItemsClass(e.Column, lvwColumnSorter.Order)); 
     else 
      this.ListOfItems.OrderBy(a => a, new CompareListItemsClass(e.Column, lvwColumnSorter.Order)); 

     this.listView1.VirtualListSize = ListOfItems.Count; 
     this.listView1.Refresh(); 
    } 
    catch { } 
} 

public class CompareListItemsClass : IComparer<ListViewItem> 
{ 
    private CaseInsensitiveComparer ObjectCompare = new CaseInsensitiveComparer(); 
    private SortOrder OrderOfSort; 
    private int ColumnToSort; 

    public CompareListItemsClass(int columnIndex, SortOrder sortOrder) 
    { 
     ColumnToSort = columnIndex; 
     OrderOfSort = sortOrder; 
    } 

    public int Compare(ListViewItem listviewX, ListViewItem listviewY) 
    { 
     // Compare the two items 
     try 
     { 
      compareResult = ObjectCompare.Compare(
       int.Parse(listviewX.SubItems[ColumnToSort].Text), 
       int.Parse(listviewY.SubItems[ColumnToSort].Text)); 
     } 
     catch 
     { 
      compareResult = ObjectCompare.Compare(listviewX.SubItems[ColumnToSort].Text, 
       listviewY.SubItems[ColumnToSort].Text); 
     } 


     // Calculate correct return value based on object comparison 
     if (OrderOfSort == SortOrder.Ascending) 
     { 
      // Ascending sort is selected, return normal result of compare operation 
      return compareResult; 
     } 
     else if (OrderOfSort == SortOrder.Descending) 
     { 
      // Descending sort is selected, return negative result of compare operation 
      return (-compareResult); 
     } 
     else 
     { 
      // Return '0' to indicate they are equal 
      return 0; 
     } 
    } 

    int IComparer<ListViewItem>.Compare(ListViewItem x, ListViewItem y) 
    { 
     throw new NotImplementedException(); 
    } 
} 

但是什麼也沒有發生!我期望至少有一個例外是拋出!
你能指導我嗎?
謝謝。

+2

你怎麼會看到一個異常,如果它被拋出,當你只是「吃」的例外(因此空的捕獲)。 – 2010-01-16 22:12:35

+0

我已經添加了一箇中斷點 – 2010-01-16 22:21:25

回答

4

LINQ方法不影響集合,它們返回一個新集合。您可以使用ToList方法把結果變成一個列表,併爲其分配回列表變量:

this.ListOfItems = this.ListOfItems.OrderBy(a => a, new CompareListItemsClass(e.Column, lvwColumnSorter.Order)).ToList(); 
4

OrderByOrderByDescending擴展方法不會修改集合,它們會按照指定的順序返回集合的一系列項目。在你的代碼中,你只是丟棄這些方法的結果,所以什麼也沒有發生......

如果您需要對「就地」(即修改項目的實際順序)進行排序,請使用List<T>.Sort方法。


編輯

要實現下降和上升的順序,它實際上是更容易被Comparison<T>比使用IComparer<T>做到這一點,因爲你只需要否定的結果......下面是一個例子:

Comparison<ListViewItem> temp = (x, y) => 
{ 
    int xvalue = int.Parse(x.SubItems[e.Column].Text); 
    int yvalue = int.Parse(y.SubItems[e.Column].Text); 
    return xvalue.CompareTo(yvalue); 
}; 

Comparison<ListViewItem> comparison; 
if (lvwColumnSorter.Order == SortOrder.Ascending) 
{ 
    comparison = temp; 
} 
else 
{ 
    comparison = (x, y) => -temp(x, y); 
} 

this.ListOfItems.Sort(comparison); 
+0

您可以發佈一個示例代碼降序和升序排序與列表。排序? – 2010-01-16 22:13:09

+0

謝謝Thomas Levesque – 2010-01-17 04:58:27

相關問題