How to sort 2D array in Java

My code :

        Arrays.sort(intervals, new Comparator<int[]>(){
            @Override
            public int compare( int[] a, int[] b ){
                return a[0]==b[0] ? a[1] - b[1] : a[0] - b[0];
                // return Integer.compare( a[0], b[0] );
            }
        });

And here is an simple way:

Arrays.sort(myArr, (a, b) -> a[0] - b[0]);

36 Replies to “How to sort 2D array in Java”

  1. I think this is one of the modt important information for me.

    And i aam glad reading your article. But wanna remark
    on few general things, The web site style is perfect,
    the articles is really great!

  2. Greetings! I know this is kind of off topic but I was wondering which blog platform are you using for this
    site? I’m getting tired of WordPress because I’ve had problems
    with hackers and I’m looking at alternatives for another
    platform. I would be great if you could point me in the direction of a good platform.

  3. Taxi moto line
    128 Rue la Boétie
    75008 Paris
    +33 6 51 612 712  

    Taxi moto paris

    Quality articles is the important to attract the viewers
    to visit the web site, that’s what this website is providing.

  4. Very nice post. I just stumbled upon your blog and wanted
    to say that I’ve truly enjoyed surfing around your bblog posts.
    In any case I will bee subscribing to your feed and I hope you write again very soon!

Leave a Reply

Your email address will not be published. Required fields are marked *