2

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

7
  • 1
    I still need to see the code you're trying to compile. It looks like you put the call to sort somewhere where expressions aren't syntactically valid (like outside a method definition for example). Commented Jul 4, 2010 at 2:40
  • 1
    Looks like you're missing some punctuation somewhere. Commented Jul 4, 2010 at 2:41
  • I was putting the code within my class declaration however putting it right after my import cut it down to 3 errors: pastie.org/1029778 Commented Jul 4, 2010 at 2:54
  • @khan0: You need to put it inside a method. Commented Jul 4, 2010 at 3:00
  • 2
    @khan0: May I also suggest that you won't ever properly understand Java if you just randomly copy and paste other people's code fragment. Read a good text book or Sun's online Java tutorials. Commented Jul 4, 2010 at 3:04

1 Answer 1

12

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));
        }
    }   
}
Sign up to request clarification or add additional context in comments.

5 Comments

Yikes, my compiler throws a bunch of errors. Is it because I am importing: import java.util.*;
@khan0: It's not because you're importing java.util.*. That's fine. Please edit your question to show the whole code that causes the error and the error message. And your java version.
Ummm ... (potentially) creating two Integer instances so that you can use the compareTo method seems a bit wasteful.
Thanks a lot @sepp2k, it works now. @Stephen C, I will take your advice and get started on some reading soon times. Thanks a lot guys!
The whole if/else block can be removed and replaced with return x.length-y.length. The value doesn't matter, just whether it's zero, positive or negative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.