[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 597
  • Last Modified:

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
0
tbboyett
Asked:
tbboyett
  • 2
1 Solution
 
borislavmarkovCommented:
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("completed");
        v.add("started");
        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"));
        Collections.sort(v2);
        System.out.println("sorted2:" + v2);

    }
    public static class CompareStatusesCustom implements Comparator {
        public int compare(Object o1, Object o2)
        {
            return String.CASE_INSENSITIVE_ORDER.compare(String.valueOf(o1),String.valueOf(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)
        {
            return String.CASE_INSENSITIVE_ORDER.compare(toString(),String.valueOf(o));
        }
    }
}
0
 
CEHJCommented:
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());
                              default:
                                    return Integer.MIN_VALUE;
                        }
                        
                  }
            }
            
            
0
 
CEHJCommented:
:-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now