-2

I have a 2 dimensional array, that is a 2D array. I don't want to flatten out this array and sort. Basically i want to sort it by column.

Here is what i want

int [,] numbers = new int[4,3] { { -1,3,5 }, {5,6,8 },{-1,0,2},{2,3,5} };
o/p expected = [{-1,0,2},{ -1,3,5 },{2,3,5},{5,6,8 }

I am not able to get a good explanation because most of the blogs i came across flatten out the array and then sort all the elements which is not what i want. Any info is highly appreciated

I can think of sorting first item of each array and then sort it . If first item is same, then look for second one and so on..but not getting the right syntax

user3921104
  • 154
  • 13

1 Answers1

0

So the concept here is to get the transpose of the original array first and then sort the transpose row-wise and then again get the transpose of this sorted matrix.

I have tested it locally its working as expected.

using System;

internal class Matrix
{

    // Function to find the transpose
    // of the matrix mat[]
    internal static int[][] transpose(int[][] mat, int row, int col)
    {

        int[][] tr = RectangularArrays.RectangularIntArray(col, row);

        // Traverse each row of the matrix
        for (int i = 0; i < row; i++)
        {

            // Traverse each column of the matrix
            for (int j = 0; j < col; j++)
            {

                // Transpose matrix elements
                tr[j][i] = mat[i][j];
            }
        }
        return tr;
    }

    // Function to sort the given
    // matrix in row wise manner
    internal static void RowWiseSort(int[][] B)
    {

        // Traverse the row
        for (int i = 0; i < (int)B.Length; i++)
        {

            // Row - Wise Sorting
            Array.Sort(B[i]);
        }
    }

    // Function to print the matrix
    // in column wise sorted manner
    internal static void sortCol(int[][] mat, int N, int M)
    {

        // Function call to find transpose
        // of the matrix mat[][]
        int[][] B = transpose(mat, N, M);

        // Sorting the matrix row-wise
        RowWiseSort(B);

        // Calculate transpose of B[][]
        mat = transpose(B, M, N);

        // Print the matrix mat[][]
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M; j++)
            {
                Console.Write(mat[i][j] + " ");
            }
            Console.WriteLine();
        }
    }

    // Driver Code
    public static void Main(string[] args)
    {

        // Input
        int[][] mat = new int[][]
        {
        new int[] {1, 6, 10},
        new int[] {8, 5, 9},
        new int[] {9, 4, 15},
        new int[] {7, 3, 60}
        };

        int N = mat.Length;
        int M = mat[0].Length;

        // Function call to print the matrix
        // in column wise sorted manner
        sortCol(mat, N, M);

        Console.Read();
    }
}

internal static class RectangularArrays
{
    public static int[][] RectangularIntArray(int size1, int size2)
    {
        int[][] newArray = new int[size1][];
        for (int array1 = 0; array1 < size1; array1++)
        {
            newArray[array1] = new int[size2];
        }

        return newArray;
    }
}

you can also use bubble sort to row the matrix row-wise if you don't want to using Array.Sort method, an example is here.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197