排序2D锯齿状数组 [英] Sort 2d jagged array

查看:104
本文介绍了排序2D锯齿状数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对巨大的字符串数组进行排序.
我可以对它进行排序,但只能按列排序(y轴).有没有一种光滑的方法可以逐行(z轴)执行此操作?

我在列上排序的代码是这样的,并且在数据类型上是动态的.

I am trying to Sort a huge string array.
I can sort it but only column wise (y-axis). Is there a smooth way to do this by row instead (z-axis)?

my code for sorting on columns is like this and it is dynamic on datatypes.

public Program()
{
    int[][] ex_arr = new int[][] { new int[] { 32, 22,0 }, new int[] { 33, 4, 12 }, new int[] { 9,0,0 } };
   Sort<int>(ex_arr, 0);
}

public  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]));
}



使用此代码,我对(Y)列(32,33,9)进行排序.
如何在第二个数组的第(X)行上排序? (33,4,12).
实际数组太大,无法在此处发布.
任何帮助表示赞赏.谢谢.



With this code I sort on the column(Y) (32,33,9).
How can i sort on row (X) in the second array? (33,4,12).
The real array is too big to post here.
Any help is appreciated. Thanks.

推荐答案

尝试LINQ遍历数组的元素并对每个元素进行排序..

Try LINQ to go through the elements of the array and sort each one ..

int[][] ex_arr = new int[][] { new int[] { 32, 22, 0 }, new int[] { 33, 4, 12 }, new int[] { 9, 0, 0 } };
            var qry = from x in ex_arr
                      select Sort(x);
            int[][] sorted=  qry.ToArray<int[]>();

>

添加此功能以对数组进行排序...可能为此功能内置了一个功能...



add this function for sorting an array ... there might be a built in function for this ...

int[] Sort(int[] arr)
        {
            Array.Sort(arr);
            return arr;
        }



希望这对您有帮助



hope this helps


我的解决方案是在问题中使用我的代码.
我将y与x交换到一个新数组中,对其进行了排序,然后将排序后的数据插入到原始数组中.因为我找不到按行排序的方法,所以只能按列.
它的代码不是很好,也不干净,但我必须忍受...
My solution was to use my code in the question.
I swapped the y with the x into a new array,sorted it and inserted the sorted data into the original array. Because i can''t find a way to sort by rows, only columns.
The code for it is not nice and it''s not clean but i have to live with it...


这篇关于排序2D锯齿状数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆