2

I have a class which has a String field called name. I have an array of type SomeClass[] and I want to iterate through these SomeClass objects in String order on their names.

I'm curious as to what the most efficient way to do this would be. Should I use a comparator of some sort? Would it be a good idea to put them all into a TreeMap and then iterate through that or something similar? I'm sure I could come up with a solution, but I'm also sure that it would be less than efficient.

Any help is appreciated.

1
  • As per my understanding, you want to sort the class objects according to their name fields. is it ? Commented Feb 24, 2012 at 5:58

4 Answers 4

4

You can just Arrays.sort your Comparable class, like Arrays.sort(a) (see the code)

Or, if you wanted to use Collections framework

Arrays.asList(...) and Collections.sort(..) is the key.


IF SomeClass is like this

public class SomeClass implements Comparable<SomeClass>{

    public String val;

    @Override
    public int compareTo(SomeClass that) {
        return this.val.compareTo(that.val);
    }

    @Override
    public String toString() {
        return this.val;
    }
}

yo can sort like this

    SomeClass o = new SomeClass();
    o.val = "z";
    SomeClass t = new SomeClass();
    t.val = "a";
    SomeClass th = new SomeClass();
    th.val = "m";
    SomeClass[] a = new SomeClass[]{o, t, th};

    //this
    Arrays.sort(a);

    //or this
    List<SomeClass> l = Arrays.asList(a);
    System.out.println(l);
    Collections.sort(l);
    System.out.println(l);
Sign up to request clarification or add additional context in comments.

1 Comment

@programmer_1 for sake of simplicity I have made the attribute public. :) It serves the purpose for example.
3

Put your Classes in an Arraylist and use its sort method. Unverified code:

yourArray.sort(new YourNameComparator());

class YourNameComparator implements Comparator<YourNameClass> {
    int compare(YourNameClass y1, YourNameClass y2) {
        return y1.getName().compareTo(y2.getName());
    }
}

1 Comment

this is more what I was looking for as I couldn't change the SomeClass class, and didn't want to have to subclass it. I ended up doing this but just with an anonymous Comparator class right in the sort() call. Thanks.
1

In my opinion, your best bet would be to, as you say, put it in a TreeMap (or some data structure that sorts it for you) and then read it out already sorted. There's hardly a way to get faster, and this way would produce the cleanest and most readable code.

Comments

1

Your two options: have your class implement comparable and write a compareTo method or create a comparator and feed that into Collections.sort(List list, Comparator c)

Since you want to compare strings, you probably want to use comparable since StringY.compareTo(String x) already exists.

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html

Comments

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.