使用百分比值

时间:2017-04-20 06:46:57

标签: c# forms sorting listview

我已经编写了一个通用方法,通过单击要排序的列标题对列表视图进行排序。只需很少的代码就能很好地工作。它还通过默认数字识别并正确排序。 但是,我也希望能够对百分比进行排序,是否有人知道如何做到最好?我认为它应该能够以某种方式对角色上的项目进行排序,但是从右到左。

通用分拣机的代码如下所示:

private void GenericListviewColumnSorter(object sender, ColumnClickEventArgs e)
{
    ListViewColumnSorter listViewSorter = ((ListViewColumnSorter)((ListView)sender).ListViewItemSorter);

    if (listViewSorter == null)
    {
        ((ListView)sender).ListViewItemSorter = new ListViewColumnSorter();
        listViewSorter = ((ListViewColumnSorter)((ListView)sender).ListViewItemSorter);
    }

    listViewSorter.SortColumn = e.Column;

    if (listViewSorter.Order == SortOrder.Ascending) listViewSorter.Order = SortOrder.Descending;
    else listViewSorter.Order = SortOrder.Ascending;

    (sender as ListView).Sort();
}

按百分比排序的方式如下:
90%
60%
5%
12%
100%
100%
0%

修改

ListViewColumnSorter类与the microsoft support page中描述的基本相同。

using System.Collections;
using System.Windows.Forms;

/// <summary>
/// This class is an implementation of the 'IComparer' interface.
/// </summary>
public class ListViewColumnSorter : IComparer
{
/// <summary>
/// Specifies the column to be sorted
/// </summary>
private int ColumnToSort;
/// <summary>
/// Specifies the order in which to sort (i.e. 'Ascending').
/// </summary>
private SortOrder OrderOfSort;
/// <summary>
/// Case insensitive comparer object
/// </summary>
private CaseInsensitiveComparer ObjectCompare;

/// <summary>
/// Class constructor.  Initializes various elements
/// </summary>
public ListViewColumnSorter()
{
// Initialize the column to '0'
ColumnToSort = 0;

// Initialize the sort order to 'none'
OrderOfSort = SortOrder.None;

// Initialize the CaseInsensitiveComparer object
ObjectCompare = new CaseInsensitiveComparer();
}

/// <summary>
/// This method is inherited from the IComparer interface.  It compares the two objects passed using a case insensitive comparison.
/// </summary>
/// <param name="x">First object to be compared</param>
/// <param name="y">Second object to be compared</param>
/// <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns>
public int Compare(object x, object y)
{
int compareResult;
ListViewItem listviewX, listviewY;

// Cast the objects to be compared to ListViewItem objects
listviewX = (ListViewItem)x;
listviewY = (ListViewItem)y;

// Compare the two items
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;
}
}

/// <summary>
/// Gets or sets the number of the column to which to apply the sorting operation (Defaults to '0').
/// </summary>
public int SortColumn
{
set
{
ColumnToSort = value;
}
get
{
return ColumnToSort;
}
}

/// <summary>
/// Gets or sets the order of sorting to apply (for example, 'Ascending' or 'Descending').
/// </summary>
public SortOrder Order
{
set
{
OrderOfSort = value;
}
get
{
return OrderOfSort;
}
}

}

1 个答案:

答案 0 :(得分:2)

好的问题在于比较,它基于字符串而不是int。这很容易修复,因为ObjectCompare.Compare会返回-1,0或1来告诉您它是否更少,等于或者更多&#39;。我们可以通过自己的比较轻松返回这些数字。

更改此部分:

int compareResult;
ListViewItem listviewX, listviewY;

// Cast the objects to be compared to ListViewItem objects
listviewX = (ListViewItem)x;
listviewY = (ListViewItem)y;

// Compare the two items
compareResult = ObjectCompare
                   .Compare
                   (listviewX.SubItems[ColumnToSort].Text,
                    listviewY.SubItems[ColumnToSort].Text);

对此:

int compareResult = 0;//Give value because if/else
ListViewItem listviewX, listviewY;

// Cast the objects to be compared to ListViewItem objects
listviewX = (ListViewItem)x;
listviewY = (ListViewItem)y;

Regex percentageExpr = new Regex(@"^([1-9]?[0-9]|100)( %|%)");

// Compare the two items
if (percentageExpr.IsMatch(listviewY.SubItems[ColumnToSort].Tex‌​t) 
 && percentageExpr.IsMatch(listviewX.SubItems[ColumnToSort].Text‌​))
{
    int textY = Convert.ToInt32( listviewY.SubItems[ColumnToSort]
                                 .Text.Replace( "%", String.Empty ) );
    int textX = Convert.ToInt32( listviewX.SubItems[ColumnToSort]
                                 .Text.Replace( "%", String.Empty ) );

    compareResult =
    ( textX > textY )
        ? 1
        : textX == textY
                ? 0 : -1;
}
else
{
    compareResult = ObjectCompare
                   .Compare
                   (listviewX.SubItems[ColumnToSort].Text
                   , listviewY.SubItems[ColumnToSort].Text);
}
相关问题