English 中文(简体)
在C#中,我是如何用行 的阵列?
原标题:How do I sort jagged array by row in C#?

我的阵列有2D。 我想用几句子来分类。

I 检索并找到按栏分类的代码

private static void Sort<T>(T[][] data, int col) 
{ 
    Comparer<T> comparer = Comparer<T>.Default;
    Array.Sort<T[]>(data, (x,y) => comparer.Compare(x[col],y[col])); 
}

我能否用几句子来加以调整?

Any help is appreciated.

我gged的阵列(Added)样本

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 10;
            int[][] capm = new int[3][];
            for (int i = 0; i <= 2; i++)
            {
                capm[i] = new int[n + 1];
            }
            Random rand = new Random();            
            for (int i = 1; i <= n; i++)
            {
                capm[1][i] = i;
            }

            for (int i = 1; i <= n; i++)
            {
                capm[2][i] = rand.Next(1, 6);
            }

            Sort(capm, 2);

            Console.ReadLine();
        }
            private static void Sort<T>(T[][] data, int col)    
            {  
                data = data.OrderBy(i => i[col]).ToArray();
            }
        }

    }

@Dani &@Martin 我希望我的阵列能够用帽子安插。

问题回答

我认为这样做的唯一途径是采用一系列指数:

private static void Sort<T>(T[][] data, int row) 
{
    int[] Indices = new int[data[0].Length];
    for(int i = 0; i < Indices.Length; i++)
        Indices[i] = i;

    Comparer<T> comparer = Comparer<T>.Default;
    Array.Sort(Indices, (x, y) => comparer.Compare(data[row][x], data[row][y]);

    for(int i = 0; i < data.Length; i++)
    {
        T[] OldRow = (T[])data[i].Clone();
        for(int j = 0; j < OldRow.Length; j++)
            data[i][j] = OldRow[i][Indices[j]];
    }
}

鉴于你正在使用一个挂号的阵列,因此在第3个项目之前就会出现这种情况,但是如果你想要保证每个行各有相同的栏目,则2D阵列可能更好。 如果您的阵列内有一阵列,那么这就会失败!

private static void Sort<T>(T[][] data, int col)    
{  
    data = data.OrderBy(i => i[col]).ToArray();
}

Edit:

In order to do anything with the new data reference you either need to return it or pass the parameter by reference:

private static void Sort<T>(ref T[][] data, int col)    
{  
    data = data.OrderBy(i => i[col]).ToArray();
}

阵列本身是固定的,创造了一种新的阵列。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签