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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:42:39+00:00 2026-05-15T16:42:39+00:00

I have an arraylist of String[]: ArrayList< String [] > mystuff = new ArrayList

  • 0

I have an arraylist of String[]:

ArrayList< String [] > mystuff = new ArrayList < String [] > ();

I want to sort them in largest-array-size ascending order. Example:

mystuff = {[“this”, “is”, “item”,
“one”], [“this”, “is”, “item”, “two”],
[“item”], [“item”, “three”]}

Should become:

mystuff = {[“item”], [“item”,
“three”], [“this”, “is”, “item”,
“one”], [“this”, “is”, “item”, “two”]}

For arrays of equal length, the order doesn’t matter.

Edit:

Java compiler version: javac 1.6.0_20

Error that I am facing by using
@sepp2k’s code:
http://pastie.org/private/ienpdtj0ft6czw6nboeva

  • 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-15T16:42:40+00:00Added an answer on May 15, 2026 at 4:42 pm

    Use Collections.sort with a Comparator that compares the length.

    Collections.sort(mystuff, new Comparator<String[]>() {
        public int compare(String[] x, String[] y) {
            if(x.length < y.length) {
                return -1;
            } else if(x.length == y.length) {
                return 0;
            } else {
                return 1;
            }
        }
    });
    

    Edit: Here’s a complete class that compiles and runs without error (and shows the correct result):

    import java.util.*;
    
    public class Bla {                         
        public static void main(String[] args) {
            // Create list
            List<String[]> mystuff = new ArrayList<String[]>();
            mystuff.add(new String[] {"lilu", "lolo"});
            mystuff.add(new String[] {"lala"});
            mystuff.add(new String[] {"lila", "blabla", "pfirsichkuchen"});
    
            // Sort list
            Collections.sort(mystuff, new Comparator<String[]>() {
                public int compare(String[] x, String[] y) {
                    if(x.length < y.length) {
                        return -1;
                    } else if(x.length == y.length) {
                        return 0;
                    } else {
                        return 1;
                    }
                }
            });
    
            // Output list
            for(String[] strs : mystuff) {
                System.out.println(Arrays.toString(strs));
            }
        }   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have arraylist myorderdata . I want to retrieve this arraylist to one String
I have an ArrayList of HashMap like this: ArrayList<HashMap<String, String>> playListSongs = new ArrayList<HashMap<String,
I have this code: FVDTO.setStatus(fail); List<String[]> invalidFields = new ArrayList<String[]>(); Iterator<ConstraintViolation<HazardSubmission>> iterator = cv.iterator();
I have a ConcurrentHashMap in the below format: ConcurrentHashMap<String,ConcurrentHashMap<String,ArrayList>> Now in this map,I want
I have an arraylist called 'pri1', I want this array list to be available
I have this arraylist: ArrayList ftd = new ArrayList(); ftd.Add(new string[]{ foo1.txt, 01.01.2011 });
I have an arraylist<string> of words. I sort it using Collections.sort(wordsList); I'm using this
I have an ArrayList of strings, and I want to randomly change a string's
I have wriiten a method like this public ArrayList<T> GetDoctorDetail(String name) { if (name!=null
I have this method to display the contact numbers in my inbox: public ArrayList<String>

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.