2017-04-09 199 views
1

我有c#中的列表視圖排序問題,我使用的代碼從MSDN: https://msdn.microsoft.com/en-us/library/ms996467.aspx。 問題是如果你有一個2位數的數字,它不能正確地排序值? enter image description hereSorting listview does not work with 2 digits c#

請幫忙嗎?

編輯:

public class ListViewItemComparer : IComparer 
    { 
     private int col; 
     private SortOrder order; 
     public ListViewItemComparer() 
     { 
      col = 0; 
      order = SortOrder.Ascending; 
     } 
     public ListViewItemComparer(int column, SortOrder order) 
     { 
      col = column; 
      this.order = order; 
     } 
     public int Compare(object x, object y) 
     { 
      int returnVal = -1; 
      returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text, 
            ((ListViewItem)y).SubItems[col].Text); 
      // Determine whether the sort order is descending. 
      if (order == SortOrder.Descending) 
       // Invert the value returned by String.Compare. 
       returnVal *= -1; 
      return returnVal; 
     } 
    } 

和:

private int sortColumn = -1; 
     private void listView1_ColumnClick(object sender, ColumnClickEventArgs e) 
     { 
      // Determine whether the column is the same as the last column clicked. 
      if (e.Column != sortColumn) 
      { 
       // Set the sort column to the new column. 
       sortColumn = e.Column; 
       // Set the sort order to ascending by default. 
       listView1.Sorting = SortOrder.Ascending; 
      } 
      else 
      { 
       // Determine what the last sort order was and change it. 
       if (listView1.Sorting == SortOrder.Ascending) 
        listView1.Sorting = SortOrder.Descending; 
       else 
        listView1.Sorting = SortOrder.Ascending; 
      } 

      // Call the sort method to manually sort. 
      listView1.Sort(); 
      // Set the ListViewItemSorter property to a new ListViewItemComparer 
      // object. 
      this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column, 
                  listView1.Sorting); 
     } 
+0

發佈您的代碼。 – Sami

+0

Oki,謝謝,我添加了我的代碼 –

+0

您正在尋找[自然分類](http://www.interact-sw.co.uk/iangblog/2007/12/13/natural-sorting) – Steve

回答

2

在你鏈接到MSDN上的例子,這是代碼:

public int Compare(object x, object y) 
{ 
    int returnVal = -1; 
    returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text, 
    ((ListViewItem)y).SubItems[col].Text); 
    return returnVal; 
} 

這是做了string比較所以你的專欄被排序爲string。您需要修改代碼,以便類型爲int或其他數字類型以使用數字對其進行排序。以下是一些代碼:

public int Compare(object x, object y) 
{ 
int returnVal = -1; 
    try 
    { 
     // Parse the two objects passed as a parameter as a DateTime. 
     int num1 = 
      int.Parse(((ListViewItem)x).SubItems[col].Text); 
     int num2 = 
       int.Parse(((ListViewItem)y).SubItems[col].Text); 
     // Compare the two numbers. 
     returnVal = num1.CompareTo(num2); 
    } 
    // If neither compared object has a valid int, compare 
    // as a string. 
    catch 
    { 
     // Compare the two items as a string. 
     returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text, 
        ((ListViewItem)y).SubItems[col].Text); 
    } 
    // Determine whether the sort order is descending. 
      if (order == SortOrder.Descending) 
       // Invert the value returned by String.Compare. 
       returnVal *= -1; 
      return returnVal; 

} 
+0

對不起,但它不工作,比較它沒有定義fot int –

+0

什麼不行?你不能只複製粘貼代碼,試着理解代碼並看看它在做什麼。然後調整代碼以滿足您的需求。請花幾分鐘時間瞭解代碼並查看可能存在的問題,因爲問題可能在其他地方。 – CodingYoshi

+0

有沒有方法比較int,好方法是:num1.compareTo(num2) –