Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6782581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:45:38+00:00 2026-05-26T16:45:38+00:00

For example: My first column in the array is at first numerically in order.

  • 0

For example:

My first column in the array is at first numerically in order. The Second column is numerically random. I want to place the second column in order, which of course will force the first column out of order. Any ideas how to do this in java. I have searched, but can’t seem to find the vocabulary to properly find it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:45:39+00:00Added an answer on May 26, 2026 at 4:45 pm

    The method you can use is one of the Arrays.sort(), where you define your own comparator, which can compare two rows of a two-dimensional array.

    The code below allocates an array with two columns and ten rows, with the first column sorted and the second one completely random. Then it sorts it according to the second column.

    class Test{
    
        static void print(int[][] a){
            for (int i=0;i<a.length;i++){
                for (int j=0;j<a[0].length;j++) 
                    System.out.print(a[i][j]+" ");
                System.out.println();
            }
        }
    
        public static void main(String[]args){
    
            java.util.Random r = new java.util.Random();
            int[][] a = new int[10][2];
            for (int i=0;i<a.length;i++){
                a[i][0]=i+1;
                a[i][1]=r.nextInt(100);
            }
    
            print(a);
            System.out.println();
    
            java.util.Arrays.sort(a, 
                new java.util.Comparator<int[]>(){
                    public int compare(int[]a,int[]b){
                        return a[1]-b[1];
                    }
            });
    
            print(a);
        }
    }
    

    Example output:

    1 8 
    2 49 
    3 82 
    4 89 
    5 8 
    6 0 
    7 83 
    8 89 
    9 8 
    10 46 
    
    6 0 
    1 8 
    5 8 
    9 8 
    10 46 
    2 49 
    3 82 
    7 83 
    4 89 
    8 89 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

example some array {2,8,9,10,21,32,1,6,3...} first child take (data size / 2) and sort second
Is there any Java library that supports hierarchical column? For example (the first three
I need to set/get the cookies stored at first.example while browsing second.example , I
For example, will the first piece of code perform a full search twice, or
I have two columns, the first column will have the name of a object,
For example: First, say I have a Silverlight app with Windowless=true so that I
When attempting to run the first example in the boost::mpi tutorial , I was
I have a simple marker annotation for methods (similar to the first example in
Could someone explain this (strange) behavior? Why is the length in the first example
Jeff's recent article linked to a time management example of the First Fit Decreasing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.