对包含空元素的交错数组进行排序

发布于 2024-09-13 10:18:17 字数 721 浏览 2 评论 0原文

我声明了一个锯齿状数组,就像

int[][][] tmpA = new int[INT_WORKING_SIZE * 2][][];

我尝试使用以下代码对该数组进行排序:

Array.Sort(tmpA, 0, INT_WORKING_SIZE*2, new MyArrayComparer());

和我的类:

  public int Compare(object x,object y)
    {
        if (x == null || y == null)
            return 0;
        int[][] arrayA = (int[][])x;
        int[][] arrayB = (int[][])y;

         int resultA = arrayA[1].Sum();
         int resultB = arrayB[1].Sum();

        return resultA.CompareTo(resultB);          
    }

锯齿状数组的每一行都有 2 个包含 12 个整数的数组。

我想通过添加第二个数组的所有 12 个整数来对数组进行排序,并且最小的应该在第一个。

然而我的主要问题是对象 x,y 通常为空,并且排序后的数组全为零。

有什么建议吗?

I have a jagged array declared like

int[][][] tmpA = new int[INT_WORKING_SIZE * 2][][];

I trying to sort this array with this code:

Array.Sort(tmpA, 0, INT_WORKING_SIZE*2, new MyArrayComparer());

and my class:

  public int Compare(object x,object y)
    {
        if (x == null || y == null)
            return 0;
        int[][] arrayA = (int[][])x;
        int[][] arrayB = (int[][])y;

         int resultA = arrayA[1].Sum();
         int resultB = arrayB[1].Sum();

        return resultA.CompareTo(resultB);          
    }

each row of jagged array has 2 arrays with 12 ints.

I want to sort the array by adding all the 12 ints of the second array and the smallest should be first.

However my major problem is that object x,y are often nulls and the sorted array gets all zeros.

any tips?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉城メ夜风 2024-09-20 10:18:17

如果我理解正确,你的问题是当两个数组中的任何一个为空时你返回 0,当你应该返回 1 或 -1 取决于哪个不为空时,只有当两个数组都为空时才返回 0。

public int Compare(object x,object y)
{
    // changed code
    if (x == null && y == null)
        return 0;
    if (x == null)
        return 1;
    if (y == null)
        return -1;
    // end of changed code
    int[][] arrayA = (int[][])x;
    int[][] arrayB = (int[][])y;

     int resultA = arrayA[1].Sum();
     int resultB = arrayB[1].Sum();

    return resultA.CompareTo(resultB);          
}

If I understand you correctly, your problem is you're returning 0 when either of the arrays is null, when you should be returning 1 or -1 depending on which is not null, and 0 only when both are null.

public int Compare(object x,object y)
{
    // changed code
    if (x == null && y == null)
        return 0;
    if (x == null)
        return 1;
    if (y == null)
        return -1;
    // end of changed code
    int[][] arrayA = (int[][])x;
    int[][] arrayB = (int[][])y;

     int resultA = arrayA[1].Sum();
     int resultB = arrayB[1].Sum();

    return resultA.CompareTo(resultB);          
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文