Java Program to Sort 2D Array Across Columns Last Updated : 18 Mar, 2022 Comments Improve Suggest changes Like Article Like Report The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in the java.util package and implements the List interface, so we can use all the methods of List interface here. This program is used to Sort the 2D array Across Columns. We will use the concept of vector to sort each column. Algorithm: Traverse each column one by one.Add elements of Column 1 in vector v.Sort the vector.Push back the sorted elements from vector to column.Empty the vector by removing all elements for fresh sorting.Repeat the above steps until all columns are done. Illustration: Creating a Vector Here we are creating a default vector of the initial capacity is 10 so do the syntax is as follows: Vector<E> v = new Vector<E>(); Functions that will be used in order to achieve the goal are as follows: A. removeAll(): The java.util.vector.removeAll(Collection col) method is used to remove all the elements from the vector, present in the collection specified. Syntax: Vector.removeAll(Vector) B. Collections.sort(): This method is used to sort the vector. Syntax: Collections.sort(Vector) C. add(): This method is used to add elements in the vector. Syntax: Vector.add(value) D. get(): This method will get the element of Vector stored at a particular index Syntax: Vector.get(3); Example Java // Java Program to Sort 2D array across Columns // Importing required classes import java.io.*; import java.lang.*; import java.util.*; // Main class class GFG { // Main driver method public static void main(String[] args) throws java.lang.Exception { // Custom input for 2D array int[][] arr = { { 7, 2, 0, 5, 1 }, { 3, 8, 2, 9, 14 }, { 5, 1, 0, 5, 2 }, { 4, 2, 6, 0, 1 } }; // Display message for better readability System.out.println("Matrix without sorting \n"); // Nested iteration to display matrix for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { // Printing elements of 2D matrix System.out.print(arr[i][j] + " "); } // New line as we are done with row System.out.println(); } // Creating an object of Vector class Vector<Integer> v = new Vector<>(); // Nested iteration using for loops for (int i = 0; i < 5; i++) { for (int j = 0; j < 4; j++) { // Adding elements of columns in vector // using add() method v.add(arr[j][i]); } // Sorting elements in vector // using sort() method Collections.sort(v); for (int j = 0; j < 4; j++) { // Sorted elements are pushed back // from vector to column arr[j][i] = v.get(j); } // Elements are removed from vector for // fresh sorting using remove() method v.removeAll(v); } // Printing matrix after sorting System.out.println("Matrix after sorting \n"); for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } } } OutputMatrix without sorting 7 2 0 5 1 3 8 2 9 14 5 1 0 5 2 4 2 6 0 1 Matrix after sorting 3 1 0 0 1 4 2 0 5 1 5 2 2 5 2 7 8 6 9 14 Auxiliary Space : O(1) Comment More infoAdvertise with us Next Article Java Program to Sort 2D Array Across Columns A akshitsaxenaa09 Follow Improve Article Tags : Java Java Programs Blogathon Blogathon-2021 Java-Array-Programs +1 More Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to 12 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read Like