BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Sorting 2D Array in Java

Last Updated: June 3, 2024 by Chaitanya Singh | Filed Under: java

In this tutorial, we will learn how to sort a 2D array in Java. As we know, a 2D array consists of rows and columns, thus we can sort the 2D array column-wise or row-wise, we will see both the programs.

1. Sorting 2D Array Column-wise

In the following program, we are sorting the given 2D array column-wise, this means that each column of the array needs to be sorted individually.

import java.util.Arrays;

public class Main {
public static void main(String[] args) {
// 2D Array
int[][] array = {
{5, 2, 9},
{1, 3, 7},
{4, 0, 8}
};

// Displaying the elements of original array
System.out.println("Original array:");
print2DArray(array);

// Sort each column of 2D Array
sortColumns(array);

// Displaying the elements of sorted array
System.out.println("\nArray sorted column-wise:");
print2DArray(array);
}

// Method to sort each column of the 2D array
public static void sortColumns(int[][] array) {
int numRows = array.length;
int numCols = array[0].length;

for (int col = 0; col < numCols; col++) {
// Extract the column into a temporary array
int[] columnArray = new int[numRows];
for (int row = 0; row < numRows; row++) {
columnArray[row] = array[row][col];
}

// Sort the temporary array
Arrays.sort(columnArray);

// Write the sorted values back into the column
for (int row = 0; row < numRows; row++) {
array[row][col] = columnArray[row];
}
}
}

// This method prints the 2D array
public static void print2DArray(int[][] array) {
for (int[] row : array) {
System.out.println(Arrays.toString(row));
}
}
}

Output:

Original array:
[5, 2, 9]
[1, 3, 7]
[4, 0, 8]

Array sorted column-wise:
[1, 0, 7]
[4, 2, 8]
[5, 3, 9]

2. Sorting 2D Array Row-wise

In the following program, we are sorting the given 2D array row-wise, this means that each row of the array needs to be sorted individually.

import java.util.Arrays;

public class Main {
public static void main(String[] args) {
// 2D Array
int[][] array = {
{5, 2, 9},
{1, 3, 7},
{4, 0, 8}
};

// Displaying the elements of original array
System.out.println("Original array:");
print2DArray(array);

// Sort each row of 2D Array
sortRows(array);

// Displaying the elements of sorted array
System.out.println("\nArray sorted row-wise:");
print2DArray(array);
}

// Method to sort each row of the 2D array
public static void sortRows(int[][] array) {
for (int[] row : array) {
Arrays.sort(row);
}
}

// This method prints the 2D Array
public static void print2DArray(int[][] array) {
for (int[] row : array) {
System.out.println(Arrays.toString(row));
}
}
}

Output:

Original array:
[5, 2, 9]
[1, 3, 7]
[4, 0, 8]

Array sorted row-wise:
[2, 5, 9]
[1, 3, 7]
[0, 4, 8]

Top Related Articles:

  1. Java Arrays Methods
  2. Difference between local, instance and static variables in Java
  3. How to Convert an array to ArrayList in java
  4. Break statement in Java with example
  5. Java Scanner class with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap