C#自定义排序。所有Ns后跟ASC订单

时间:2018-02-27 17:32:33

标签: c# sorting infragistics xamdatagrid

我正在使用Infragistics XamDataGrid处理WPF应用程序。我需要排序的列很少,如下所示。

我的值会有-1.00,N,1.73,N,-5.6,N,7 ..我需要排序如下

Asc order

  <磷>氮
  ñ
  -5.6
  -1.00
  1.73
  7

描述顺序

  

7
  1.73
  -1.00
  -5.6
  ñ
  Ñ

我只需要下面的自定义排序。无法得到我需要的东西。

public class MyCustomSort : IComparer
{
    public int Compare(object x, object y)
    {
        if (x == null || y == null) return 1;

        string x1, y1;            
        x1 = x.ToString();
        y1 = y.ToString();

        if (x1 == "N" || y1 == "N") return 1;

        return x1.CompareTo(y1);
    }
}

更新 感谢 user2023861 。完成全面测试后,我会接受他的回答。我改变了下面的比较代码。我还在测试,初步测试看起来不错。请让我知道您对此的看法。

public int Compare(object x, object y)
        {
            //put nulls first
            if (x == null) return 1;
            if (y == null) return -1;
            //put Ns second after nulls
            if (x.ToString() == "N") return -1;
            if (y.ToString() == "N") return 1;
            double doubleX;
            double doubleY;
            bool xParsed = Double.TryParse(x.ToString(), out doubleX);
            bool yParsed = Double.TryParse(y.ToString(), out doubleY);
            if(xParsed && yParsed)
            {
                // both X and Y are doubles
                return doubleX.CompareTo(doubleY);
            }

            return 1;
        }

我在下面标记了 user2023861 作为回答,因为它让我走向了正确的方向。我按照上面的说法更改了代码,到目前为止测试看起来很好。如果遇到任何问题,我会更新。

谢谢大家。

3 个答案:

答案 0 :(得分:3)

在此处阅读比较方法的返回值https://msdn.microsoft.com/en-us/library/system.collections.icomparer.compare(v=vs.110).aspx总之,结果&lt; 0表示x是第一个,结果== 0表示x == y,结果&gt; 0表示y优先。

例如,这一行:

if (x == null || y == null) return 1;

返回一个结果,指出x应该在y之后,因为结果大于零。

您的算法现在正在做的是:

if x or y is null, y comes first
if x or y is "N", y comes first
otherwise compare x as a string to y as a string and return that

编辑:这是你的答案(一定要测试一下,我还没试过)

public class MyCustomSort : IComparer
{
    public int Compare(object x, object y)
    {
        //put nulls first
        if (x == null) return 1;
        if (y == null) return -1;

        string x1, y1;            
        x1 = x.ToString();
        y1 = y.ToString();

        //put Ns second after nulls
        if (x1 == "N") return -1;
        if (y1 == "N") return 1;

        return x.CompareTo(y); //assuming these are doubles
    }
}

答案 1 :(得分:2)

我在下面的快速控制台应用程序中尝试过,看起来很有效:

List<object> lst = new List<object> { "N", "N", -5.6, -1.00, 1.73, 7 };
            var result=
            lst.OrderByDescending(item =>
            {
                double val = 0;
                if (double.TryParse(item.ToString(), out val))
                {
                    return val;
                }
                return double.MaxValue; /*dummy number*/
            }).ToList();

 foreach(var i in result)
            {
                Console.WriteLine(i);
            }

以下是OrderBy和OrderbyDescending运算符执行代码段时的屏幕截图:

enter image description here

答案 2 :(得分:1)

input是来自字符串,双打和整数的佣人。您可以按类型对值进行分组,然后应用排序。试试这段代码:

var input = new object[] {-1.00, "N", 1.73, "N", -5.6, "N", 7};

// get all strings
var allN = input.Where(n => n is string);

// now filter numerics
var ints = input.Where(x => x is double).Cast<double>();
var doubles = input.Where(x => x is int).Select(x => (double) (int) x);

按类别拆分input后,您可以将其与此代码重新组合:

var asc = allN.Concat(ints.Concat(doubles)
  .OrderBy(x => x).Cast<object>()).ToArray();
var desc = ints.Concat(doubles)
  .OrderByDescending(x => x).Cast<object>().Concat(allN).ToArray();