如何将对象强制转换为列表视图排序中的每个数字

本文关键字:排序 视图 数字 列表 对象 转换 | 更新日期: 2023-09-27 17:55:12

我已经从msdn对lst视图进行了排序

我已经输入了我单击的列:

public int Compare(object x, object y)
{
    int returnVal = new int();
        // Determine whether the type being compared is a date type.
    switch (field_type.Split('(')[0])
    {
        case "int":
        case "float":
        case "tinyiny":
        case "smallint":
        case "bigint":
        case "bit":
        case "mediumint":
        case "double":
        case "decimal":
        case "unsigned":
            returnVal = 0;
        break;
        // case "
        case "date":
        case "datetime":
            // Parse the two objects passed as a parameter as a DateTime.
            System.DateTime firstDate =
            DateTime.Parse(((ListViewItem)x).SubItems[col].Text);
            System.DateTime secondDate =
            DateTime.Parse(((ListViewItem)y).SubItems[col].Text);
            // Compare the two dates.
            returnVal = DateTime.Compare(firstDate, secondDate);
            break;
        default: // Compare the two items as a string.
            returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
            break;
    }
}

如果单击的列是数字浮点数,int,deciam ecc...如何编写一个在 2 个对象之间返回最大值的函数?

如何将对象强制转换为列表视图排序中的每个数字

对给定类型使用可用的 CompareTo 方法。例如:

case "int":
    returnValue = ((int) x).CompareTo((int) y);
public int Compare(object x, object y)
{
    var a = x as IComparable ;
    if (a == null) throw new Exception();
    return a.CompareTo(y);
}