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 6756403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:31:12+00:00 2026-05-26T13:31:12+00:00

I have a 2D-array that I want to sort based on the second column.

  • 0

I have a 2D-array that I want to sort based on the second column. The first column should remain paired with the second column.

The 2D-array is initially as follows (2×10 matrix):

0 10
1 9
2 9
3 9
4 15
5 10
6 4
7 8
8 11
9 12

I want the above 2D-array to be sorted like this:

4 15
9 12
8 11
0 10
5 10
1 9
2 9
3 9
7 8
6 4

Now, I’ve tried adapting the answer from: Sort a two dimensional array based on one column into this code:

Arrays.sort(theArray, new Comparator<Integer[]>()
{
    @Override
    public int compare(Integer[] int1, Integer[] int2)
    {
        Integer numOfKeys1 = int1[1];
        Integer numOfKeys2 = int2[1];
        return numOfKeys1.compareTo(numOfKeys2);
    }
});

However, it doesn’t seem to sort the array at all. When printing the array after calling the sort() function the array is in its initial order.

I also tried adapting the answer from here: sorting 2D array of String in java but I encountered the same problem.

Have I made some fatal mistake when adapting these solutions, or should my code work?

Also, how would I go about sorting this array in descending order? Would I replace the return statement in compare() with this line?

return -numOfKeys2.compareTo(numOfKeys1);

Any help would be greatly appreciated. Thanks!

EDIT: Just posting the rest of my code to see if the problem is elsewhere.

public void Sort()
{   
    Integer[][] theArray = {{0,10},{1,9},{2,9},{3,9},{4,15},{5,10},{6,4},{7,8},{8,11},{9,12}};;

    dump(theArray);
    Arrays.sort(theArray, new Comparator<Integer[]>()
    {
        @Override
        public int compare(Integer[] int1, Integer[] int2)
        {
            Integer numOfKeys1 = int1[1];
            Integer numOfKeys2 = int2[1];
            return numOfKeys1.compareTo(numOfKeys2);
        }
    });

    System.out.println("====");
    dump(theArray);     
}

public void dump(Integer[][] array)
{
    for(int p = 0, q = 10; p < q; p++)
    {
        System.out.println(array[p][0] + " " + array[p][1]);
    }
}

EDIT 2:

I’ve got it working. Thanks everyone for your help. I had multiple Sort() functions (an older one that wasn’t working, and the one you see above), and it turns out I was calling the wrong one, even though I thought I changed the call. Just one of those days.

Feel free to use the code above if you want to sort an array. It’s fully working now.

  • 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-26T13:31:12+00:00Added an answer on May 26, 2026 at 1:31 pm

    It works fine for me. To reverse order you’d negate the original compareTo, or swap the variables, but not both.

    We’ll probably need to see the rest of the code to understand why you’re seeing what you’re seeing; I cut and pasted your code verbatim, so odds are good the issue lies elsewhere.


    dump(theArray);
    Arrays.sort(theArray, new Comparator<Integer[]>() {
        public int compare(Integer[] int1, Integer[] int2) {
            Integer numOfKeys1 = int1[1];
            Integer numOfKeys2 = int2[1];
            return numOfKeys1.compareTo(numOfKeys2);
        }
    });
    System.out.println("================");
    dump(theArray);
    
    
    0 10 
    0 10 
    1 9 
    2 9 
    3 9 
    4 15 
    5 10 
    6 4 
    7 8 
    8 11 
    9 12 
    ================
    6 4 
    7 8 
    1 9 
    2 9 
    3 9 
    0 10 
    0 10 
    5 10 
    8 11 
    9 12 
    4 15 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array that I want to sort based on the value of
I have an array that looks like $numbers = array('first', 'second', 'third'); I want
I have an array of strings that I want to sort in order of
I have an array of objects which I want to sort based on one
I have an array that stores the order that I want to sort a
So, I have an array of objects that I want to sort like so:
I have an array that I want on multiple pages, so I made it
I have an associative array that I want to display using TileList. However, it
I have one array. I want that array to retain its value between function
I have a three-dimensional array that I want to reset to zero. It seems

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.