如何对具有 $ 符号的列表视图列进行排序?

How to sort a list view column having $ sign like amount?

我在排序时遇到了一些问题。我有来自 msd 文档的以下代码,它适用于所有字段,但不适用于以字符开头的金额字段,例如 $

     amount
      00
      44 
      
      [=10=]

此代码不对该列进行排序,因此我应该进行哪些更改,以便它也对开头带有 $ 符号的金额进行排序。

这是代码:

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 void lvwUsers_ColumnClick(Object eventSender, ColumnClickEventArgs eventArgs)
    {


        if (lvwUsers.Sorting == SortOrder.Ascending)
            lvwUsers.Sorting = SortOrder.Descending;
        else
            lvwUsers.Sorting = SortOrder.Ascending;

        lvwUsers.Sort();

        this.lvwUsers.ListViewItemSorter = new ListViewItemComparer(eventArgs.Column,
                                                          lvwUsers.Sorting);

      }

它工作正常,但不适用于带有 $ 符号的金额

那么如何对金额列带有 $ 符号的列表视图进行排序?

深思 此方法针对美元符号进行了硬编码,但可能有一个正则表达式来检测非数字字符、存储它们,然后您可以将它们重新注入到最后的字符串中。

    private List<string> sortthelist(List<string> incomingList)
    {
        List<int> convertedlist = new List<int>();
        for (int i = 0; i < incomingList.Count; i++)
        {
            convertedlist.Add(int.Parse(incomingList[i].Replace("$", "")));//convert your strings into integers
        }

        convertedlist.Sort();//sort your integers
        incomingList.Clear();//clear the existing list
        foreach (int item in convertedlist)//fill it back up
        {
            incomingList.Add("$" + item.ToString());//add the dollar sign back into the string
        }

        return incomingList;//return the sorted list
    }

您已经有了比较器。为什么不修改它以按您想要的方式排序?使用 Decimal.Parse 将字符串转换为十进制值,然后进行比较。无需修改列表数据。它还可以自适应不同的文化。

如果数据可以包含货币值以外的其他内容,您可能希望使此代码更有弹性一些以处理您获得的任何类型的值。

    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;
            decimal value1;
            Decimal.TryParse(((ListViewItem)x).SubItems[col].Text,
                             System.Globalization.NumberStyles.Currency,
                             System.Globalization.CultureInfo.CurrentUICulture, 
                             out value1);
            decimal value2;
            Decimal.TryParse(((ListViewItem)y).SubItems[col].Text,
                             System.Globalization.NumberStyles.Currency, 
                             System.Globalization.CultureInfo.CurrentUICulture, 
                             out value2);
            returnVal = Decimal.Compare(value1, value2);
            // Determine whether the sort order is descending.
            if (order == SortOrder.Descending)
                // Invert the value returned by String.Compare.
                returnVal *= -1;
            return returnVal;
        }
    }