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

  • SEARCH
  • Home
  • 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 7837799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:51:09+00:00 2026-06-02T14:51:09+00:00

I have a two dimensional array that is holding the value of 5 cards.

  • 0

I have a two dimensional array that is holding the value of 5 cards. The first element of each of the 5 arrays represents the suit of the card, and the second element represents the card value.

I want to sort the 2d array by the second element, and then by the first element while maintaining the sorted order of the second element (if that makes sense). For example all suits of ones will be lower on the sorted list than all suits of two. So for example, {{0,1},{2,1},{0,2}} should become {{0,1},{2,1},{0,2}}.

Here is what I have:

 // {{3,2}, {2,2}, {0,1}, {1,0}, {2,3}} should become 
 // {{1,0}, {0,1}, {2,2}, {3,2}, {2,3}}

 int[][] hand = {{3,2},{2,2},{0,1},{1,0},{2,3}};
 sort(hand);

 public static void sort(int[][] hand){
    Arrays.sort(hand, new Comparator<int[]>(){
        public int compare(int[] o1, int[] o2){
            return Integer.valueOf(o1[1]).compareTo(Integer.valueOf(o2[1]));
        }
    });
 }

This is outputting {{1,0},{0,1},{3,2},{2,2},{2,3}}. Does anyone have any suggestions?

  • 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-06-02T14:51:14+00:00Added an answer on June 2, 2026 at 2:51 pm

    Solution 1: sort the arrays by the second element, and then sort the arrays by the first element. Since Arrays.sort is stable, that’s equivalent to first comparing by the first element, then the second.

    Solution 2: modify your comparator as follows:

    Arrays.sort(hand, new Comparator<int[]>() {
      public int compare(int[] o1, int[] o2) {
        if (o1[0] == o2[0]) {
          return Integer.compare(o1[1], o2[1]);
        } else {
          return Integer.compare(o1[0], o2[0]);
        }
      }
    });
    

    or, with Guava (disclosure: I contribute to Guava), you can just write the comparator as

      public int compare(int[] o1, int[] o2) {
        return ComparisonChain.start()
          .compare(o1[0], o2[0])
          .compare(o1[1], o2[1])
          .result();
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a two dimensional JSON array where each element contains several attributes. The
I have the following three arrays and need to create a new two-dimensional array
I have a two dimensional array that I need to rotate 90 degrees clockwise,
I have a two dimensional array that I need to sort numerically. Here is
Assume, I have a two dimensional array A , and it's stated that somewhere
I am new to PHP. I have a PHP array that is two dimensional.
I have an single-dimensional array of PHP objects. Each object has two attributes, one
I have a two dimensional array that looks like this: TITLETYPE = [['Prof.', '4'],
I have a two dimensional array in PHP that prints the index of the
I have a two dimensional array that I need to load data into. I

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.