---
title: "Two Dimensional array with its sorting using java"  
description: "In this blog I am trying to make a two dimensional array accepted by user and the find the maximum and minimum value with its index and also show the"  
author: "Vijay Shukla"  
published: 2013-10-04  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/600/two-dimensional-array-with-its-sorting-using-java  
category: "java"  
tags: ["java"]  
reading_time: 6 minutes  

---

# Two Dimensional array with its sorting using java

In this [blog](https://www.mindstick.com/articles/12705/myths-and-misconception-about-blog) I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to make a two dimensional [array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net) [accepted](https://yourviews.mindstick.com/view/80674/whatsapp-accepted-as-most-popular-app-worldwide) by [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) and the find the maximum and minimum [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) with its [index](https://www.mindstick.com/blog/198/index-in-sql-server) and also show the [matrix](https://www.mindstick.com/forum/2412/how-to-convert-matrix-to-uiimage) with sorted (ascending) order.

\

```
import java.io.*;import java.util.Arrays;import java.util.Comparator;public class matrix2D{
    static int indexRow=0;    static int indexColumn=0;    public static void main(String[] args){
                System.out.print("Enter the number of Rows : ");                int row= Integer.parseInt(System.console().readLine());                System.out.println();                System.out.print("Enter the number of Column : ");                int col= Integer.parseInt(System.console().readLine());                int[][] matrix2D = new int[row][col];                 System.out.println("Enter the values of Matrix");                System.out.println();
                for(int i=0;i<row;i++)                {                                for(int j=0;j<col;j++)                                {                                               System.out.print("["+i+"]["+j+"] : ");
                                                matrix2D[i][j]= Integer.parseInt(System.console().readLine());
                                }         
                }
                System.out.println("::Matrix::");                System.out.println();                  for(int i=0;i<row;i++)                {                                for(int j=0;j<col;j++)                                {                                                System.out.print(matrix2D[i][j]+"\t");
                                }                                System.out.println("\n");                    
                }                System.out.println();                System.out.println("::Maximum and Minimum Values in Matrix::\n");
                System.out.println();                int minimumValue=min(matrix2D);                System.out.println("Minimum Value is "+minimumValue+" at ["+indexRow+"]["+indexColumn+"] Position");
                int maximumValue=max(matrix2D);                System.out.println("Maximum Value is "+maximumValue+" at ["+indexRow+"]["+indexColumn+"] Position");
                sortMatrix(matrix2D);
    }    public static int min(int[][] matrix)    {        int min = matrix[0][0];        for (int col = 0; col < matrix.length; col++)        {            for (int row = 0; row < matrix[col].length; row++)                    {                if (min > matrix[col][row])                                {                   min = matrix[col][row];                                   indexRow=col;                   indexColumn=row;                }            }        }
        return min;    }    public static int max(int[][] matrix)    {        int max = matrix[0][0];        for (int col = 0; col < matrix.length; col++) {            for (int row = 0; row < matrix[col].length; row++) {                if (max < matrix[col][row]) {                    max = matrix[col][row];
                                    indexRow=col;                    indexColumn=row;                }            }        }        return max;    }
    public static void sortMatrix(int[][] array)    {                for (int row = 0; row < array.length; row++)                {                for (int col = 0; col < array[0].length; col++)                                {                                for (int colTwo = col + 1; colTwo < array[0].length; colTwo++)
                                                {                                                if (array[row][colTwo] < array[row][col])
                                                swap(array, row, col, colTwo);                                }
                }
        }                System.out.println();                System.out.println("::Sorted Matrix::");                System.out.println();              
                for (int r = 0; r < array.length; r++)                {                for (int c = 0; c < array[0].length; c++)                                {                                System.out.print(array[r][c] + "\t");                }                                System.out.println("\n");        
                }
    }    public static void swap(int[][] array, int r, int c, int colTwo)    {                int temp = array[r][c];                        array[r][c] = array[r][colTwo];                array[r][colTwo] = temp;    }  
}                           
```

\

##### Output: -

![Two Dimensional array with its sorting using java](https://www.mindstick.com/blogs/f26b5333-a630-4f09-879a-23af62fe63db/images/e0b0ab3f-34ac-47f9-b4c5-075d621bb7d3.png)

---

Original Source: https://www.mindstick.com/blog/600/two-dimensional-array-with-its-sorting-using-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
