0

I am trying to sort an arraylist by string length, i know of implementing Comparator, but i was wondering if this could be done within my function, without adding any extra classes or methods? Ideally I want to output them shortest to longest, but that I can do!

Here is a snippet of the method i would like to implement the comparator with.

public static void sCompare(BufferedReader r, PrintWriter w) throws IOException {

    ArrayList<String> s= new ArrayList<String>();

    String line;
    int n = 0;
    while ((line = r.readLine()) != null) {
        s.add(line);
        n++;
    }
    //Collections.sort(s);  

    Iterator<String> i = s.iterator();
    while (i.hasNext()) {
        w.println(i.next());
    }
  }

Thanks in advance for any input!

9
  • You could implement the logic of sorting with loops, but why would you not go for comparator ? Commented Sep 22, 2013 at 3:04
  • You can use anonymous class if you want.. Commented Sep 22, 2013 at 3:05
  • @JigarJoshi i am looking for the most efficient way of doing this. And just prefer to use one class. Is there a possible efficient solution to this? Commented Sep 22, 2013 at 3:06
  • adding another class wouldn't make difference in efficiency, Collections.sort() works at n log(n) Commented Sep 22, 2013 at 3:08
  • 1
    The first part takes your file and for each line, produces its length followed by a space followed by the line. The second part sorts numerically on column 1, the length. The last part blows away the length column by replacing the digit sequence and space with nothingness. This is all pretty basic shell stuff; if it's not familiar to you that's fine. But it is so worth learning IMHO. Commented Sep 22, 2013 at 5:06

2 Answers 2

3

I don't see anything wrong with implementing the Comparator interface. If your only concern is doing everything in the function, you could use an anonymous implementation. Something along the lines of :

    Collections.sort(s, new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            return o1.length() - o2.length();
        }
    });  

(that would replace you current line //Collections.sort(s);)

PS : you never use the value of n.

PPS: You may have to invert o1 and o2 depending of the order you want in the return statement.

Another example of implementing an interface with an anonymous class

Sign up to request clarification or add additional context in comments.

6 Comments

could this be edited to handle cases where strings of same length get sortes in natural order?
Thank you for accepting my answer. What do you mean by 'natural order' ? Which order would you like ?
lets say i have {hello, heelloooo, apple, appppplleee} the output would apple hello heelloooo appppplleee... So essentially cases where length is the same, goes to alphabtical order
I see. And what would you rather have ? Anti-alphabetical for same-length words ? Or are you refering to some kind of stability ?
i would like, normal aplphabetical for same-length words
|
2

I'm going to assume by "class" you mean "top level class", thus allowing the use of an anonymous class:

Collections.sort(s, new Comparator<String>() {
    public int compare(String a, String b) {
        // java 1.7:
        return Integer.compare(a.length(), b.length());
        // java 1.6
        return a.length() - b.length();
    }
});

2 Comments

thank you for your help @Bohemian. Would you happen to know what this error means? It is from the ".compare" from the last line of code
Integer.compare was added in Java 1.7. Are you using 1.6?

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.