Sorting a vector using comparator

I have a vector of objects.  Each object contains status, from, and to.  I'm guessing I'd need to use comparator but uncertain as to how to implement it so that it will sort on any of the three criteria.

Thanks for your help
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can sort by two ways: you provide Comparator, or your objects are implementing Comparable.
In firt variant you can sort objects from any type as long as Comparator you provide is accurate(I used string sort of string representation). In second variant you can only sort objects that "knows about each other". That's why I removed strings and numbers. I think example is self talking. For more info read javadoc of Collections.sort().

import java.util.Vector;
import java.util.Comparator;
import java.util.Collections;

public class Dummy
    public static void main(String[] args)

        //WAY 1, with Comparator
        Vector v = new Vector();
        v.add(new Integer(1));
        v.add(new Object(){ public String toString(){return "unknown";}});
        Collections.sort(v,new CompareStatusesCustom());
        System.out.println("sorted:" + v);

        //Way 2, each object must implement comparable,
        //Strings and numbers objects have compareTo method
        // so we only have to make our custom object to be comparable
        Vector v2 = new Vector();
        v2.add(new MyObject("completed"));
        v2.add(new MyObject("started"));
        v2.add(new MyObject("unknown"));
        System.out.println("sorted2:" + v2);

    public static class CompareStatusesCustom implements Comparator {
        public int compare(Object o1, Object o2)

    public static class MyObject extends Object implements Comparable
        private String value;

        public MyObject(String value)
            this.value = value;

        public String toString(){return value;}

        public int compareTo(Object o)
Use a custom Comparator with a means of changing the sort field. This one sets it using the ctor, but you should also supply a setter method so you can reuse the same instance.

Collections.sort(datas, new Sorter(Sorter.SortOn.TO));


            static class Sorter implements Comparator {
                  public enum SortOn {
                        STATUS, FROM, TO;
                  private SortOn sortOn;
                  public Sorter(SortOn sortOn) {
                        this.sortOn = sortOn;
                  public int compare(Object o1, Object o2) {
                        Data d1 = (Data)o1;
                        Data d2 = (Data)o2;
                        switch(sortOn) {
                              case STATUS:
                                    return d1.getStatus().compareTo(d2.getStatus());
                              case FROM:
                                    return d1.getFrom().compareTo(d2.getFrom());
                              case TO:
                                    return d1.getFrom().compareTo(d2.getFrom());
                                    return Integer.MIN_VALUE;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.