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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:51:10+00:00 2026-05-20T20:51:10+00:00

Say, we have the following 2-dimensional array: int camels[][] = new int[n][2]; How should

  • 0

Say, we have the following 2-dimensional array:

int camels[][] = new int[n][2];

How should Java Comparator class be declared to sort the arrays by their first elements in decreasing order using Arrays.sort(camels, comparator)? The compare function for reference is:

@Override public int compare(int[] a, int [] b)
{
    return b[0] - a[0];
}
  • 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-20T20:51:11+00:00Added an answer on May 20, 2026 at 8:51 pm

    […] How should Java Comparator class be declared to sort the arrays by their first elements in decreasing order […]

    Here’s a complete example using Java 8:

    import java.util.*;
    
    public class Test {
    
        public static void main(String args[]) {
    
            int[][] twoDim = { {1, 2}, {3, 7}, {8, 9}, {4, 2}, {5, 3} };
    
            Arrays.sort(twoDim, Comparator.comparingInt(a -> a[0])
                                          .reversed());
    
            System.out.println(Arrays.deepToString(twoDim));
        }
    }
    

    Output:

    [[8, 9], [5, 3], [4, 2], [3, 7], [1, 2]]
    

    For Java 7 you can do:

    Arrays.sort(twoDim, new Comparator<int[]>() {
        @Override
        public int compare(int[] o1, int[] o2) {
            return Integer.compare(o2[0], o1[0]);
        }
    });
    

    If you unfortunate enough to work on Java 6 or older, you’d do:

    Arrays.sort(twoDim, new Comparator<int[]>() {
        @Override
        public int compare(int[] o1, int[] o2) {
            return ((Integer) o2[0]).compareTo(o1[0]);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have the following class MyComponent : IMyComponent { public MyComponent(int start_at) {...}
Say I have the following Objective-C class: @interface Foo { int someNumber; NSString *someString;
Say you have the following ANSI C code that initializes a multi-dimensional array :
Say I have the following: class Base { public Base (int n) { }
Say we have the following method: private MyObject foo = new MyObject(); // and
Say I have the following C++: char *p = new char[cb]; SOME_STRUCT *pSS =
Let's say I have the following: public void MakeMatrix(int matrixLength) { int[,] Matrix =
Let's say I have following code: int f() { int foo = 0; int
Say I have the following code, LinkedList partials = new LinkedList(); partials.add(new ArrayList()); ArrayList
Say I have the following method: void UpdateContentMethodA(Int32[] src, Int32[] dst) { for(int i=0;

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.