fork download
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7. class Array2DComparer : IComparer
  8. {
  9. public int Compare(object a, object b)
  10. {
  11. int[] x = (int[])a;
  12. int[] y = (int[])b;
  13.  
  14. for (int index = 0; index < x.Length; index++)
  15. {
  16. if (x[index] > y[index])
  17. {
  18. return 1;
  19. }
  20.  
  21. if (x[index] < y[index])
  22. {
  23. return -1;
  24. }
  25. }
  26.  
  27. return 0;
  28. }
  29. }
  30.  
  31. static int[][] ConvertToJagged(int[,] rectangularArray)
  32. {
  33. int rows = rectangularArray.GetLength(0);
  34. int cols = rectangularArray.GetLength(1);
  35.  
  36. int[][] jaggedArray = new int[rows][];
  37.  
  38. for (int row = 0; row < rows; row++)
  39. {
  40. int[] item = new int[cols];
  41.  
  42. for (int col = 0; col < cols; col++)
  43. {
  44. item[col] = rectangularArray[row, col];
  45. }
  46.  
  47. jaggedArray[row] = item;
  48. }
  49.  
  50. return jaggedArray;
  51.  
  52. // либо в одну строчку с помощью LINQ
  53. // return Enumerable.Range(0, array.GetLength(0)).Select(row => Enumerable.Range(0, array.GetLength(1)).Select(col => array[row, col]).ToArray()).ToArray();
  54. }
  55.  
  56. static int[,] ConvertToRectangular(int[][] jaggedArray)
  57. {
  58. int rows = jaggedArray.Length;
  59. int cols = jaggedArray[0].Length;
  60.  
  61. int[,] rectangularArray = new int[rows, cols];
  62.  
  63. for (int row = 0; row < rows; row++)
  64. {
  65. for (int col = 0; col < cols; col++)
  66. {
  67. rectangularArray[row, col] = jaggedArray[row][col];
  68. }
  69. }
  70.  
  71. return rectangularArray;
  72. }
  73.  
  74. static void Main(string[] args)
  75. {
  76. int[,] rectangularArray = {
  77. { 3, 0, 2 },
  78. { 3, 0, 1 },
  79. { 2, 1, 5 },
  80. { 3, 1, 6 },
  81. { 4, 2, 8 }
  82. };
  83.  
  84. int[][] jaggedArray = ConvertToJagged(rectangularArray);
  85.  
  86. Array.Sort(jaggedArray, new Array2DComparer());
  87.  
  88. foreach (var item in jaggedArray)
  89. {
  90. Console.WriteLine(string.Join(" ", item));
  91. }
  92.  
  93. rectangularArray = ConvertToRectangular(jaggedArray);
  94. }
  95. }
Success #stdin #stdout 0.02s 16380KB
stdin
Standard input is empty
stdout
2 1 5
3 0 1
3 0 2
3 1 6
4 2 8