how to sort 2d array in java

0 votes
Nov 29, 2023 in Java by Evanjalin
• 20,980 points
568 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
The Arrays class and the Comparator interface in Java can be used to sort a 2D array. Here's an easy example

import java.util.Arrays;

import java.util.Comparator;

public class Sort2DArray {

    public static void main(String[] args) {

        int[][] twoDArray = {

                {4, 3, 2, 1},

                {8, 7, 6, 5},

                {12, 11, 10, 9}

        };

System.out.println("Original 2D Array:");

        print2DArray(twoDArray);

  // Sorting the 2D array based on the first column

        Arrays.sort(twoDArray, Comparator.comparingInt(a -> a[0]));

System.out.println("\nSorted 2D Array based on the first column:");

  print2DArray(twoDArray);

    }

  // Helper method to print the 2D array

    private static void print2DArray(int[][] array) {

        for (int[] row : array) {

            for (int value : row) {

                System.out.print(value + " ");

            }

            System.out.println();

        }

    }

}

The Arrays.sort method is used to sort the 2D array in this example. The Comparator.comparingInt(a -> a[0]) method specifies that the array be sorted by the values in the first column. You can change the comparator to sort on other columns or according to your specific needs.

Take note that the array is sorted in ascending order in this example. Comparator.comparingInt(a -> a[0]) can be used to sort in descending order.Instead, use reversed(). Depending on your sorting criteria, adjust the comparator as needed.
answered Dec 4, 2023 by pooja
• 17,140 points

edited Mar 5

Related Questions In Java

0 votes
3 answers

How to sort an array in java?

import java.util.Arrays; public class Sort { ...READ MORE

answered Aug 24, 2018 in Java by Parth
• 4,640 points
1,402 views
0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,140 points
1,859 views
0 votes
1 answer

How to print java array in the simplest way?

String[] arr = new String[] {"John", "Mary", ...READ MORE

answered Apr 17, 2018 in Java by sophia
• 1,400 points
941 views
0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,920 points
13,889 views
0 votes
2 answers

How to convert array into list in Java?

Another workaround if you use apache commons-lang: int[] ...READ MORE

answered Aug 10, 2018 in Java by samarth295
• 2,220 points
1,032 views
+1 vote
3 answers

How to convert a List to an Array in Java?

Either: Foo[] array = list.toArray(new Foo[list.size()]); or: Foo[] array = ...READ MORE

answered Aug 7, 2018 in Java by Akrati
• 3,190 points
1,284 views
0 votes
2 answers

How an object array can be converted to string array in java?

System.arraycopy is the most efficient way, but ...READ MORE

answered Aug 8, 2018 in Java by Sushmita
• 6,920 points
6,099 views
0 votes
2 answers

How does random shuffling of an array

Here is a simple way using an ArrayList: List<Integer> ...READ MORE

answered Nov 2, 2018 in Java by Sushmita
• 6,920 points
1,013 views
0 votes
0 answers
0 votes
0 answers

Java : Sort integer array without using Arrays.sort()

I have this assignment- Write java program ...READ MORE

Aug 2, 2022 in Python by krishna
• 2,820 points
2,369 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP