开发者

How to sort 2D array (row-wise) in Visual Studio C#?

Need your expert guidance as how to sort a 2D array, row-wise ( means considering each row independently and sort all rows independently of a 2D array) in Visual Studio C#.Example

First row: 5 4 3 6 Second row: 2 3 1 4

Sorted: First row :3 4 5 6 Second开发者_运维百科 row: 1 2 3 4

There is a method called Array.Sort (), but it is specific to 1D arrays only. Thanks and Regards Asad


If your 2D array is a jagged array like:

int[][] foo = new int[][] { 
    new int[] { 5, 4, 3, 6 }, 
    new int[] { 2, 3, 1, 4 } };

you can do it by using LINQ:

for (int i = 0; i < foo.Length; i++)
    foo[i] = foo[i].OrderBy(s => s).ToArray();


Your example is of a 1D array, by 2D array do you mean somthing lke

int[2,2] foo;

so you'll do some thing like.

foo[0,0] = 0;
foo[0,1] = 1;
foo[1,0] = 1;
foo[1,1] = 1;

please clarify.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜