public class ObjPlcy implements Serializable { private int plcyID = 0; private Vector ObjPlcyLb = new Vector(); }
public class ObjPlyLB implements Serializable { private int plcyLBID = 0; private int plcyID = 0; private int Priority = 0; }
Now, I have created a Vector vBuffPlcy comprising of objPlcy which again have objPlcyLB that has a prority code in it. I want to sort based on the priority code and set the Vector of ObjPlcy based on the same. I have written the logic below, and it is not working in all scenarios.
Could someone throw light on the same.
while(vBuffPolicy.size() > 0) { int iPos = 0; // use the first element for comparison ObjPlcy oPol1 = (ObjPlcy)vBuffPolicy.firstElement();
Forget this -- instead look at the static java.util.Collections.sort() methods, and the java.util.Comparator interface. It's silly to write your own sorting method -- the easy ones are slow, and the fast ones are hard to get right.
The classes, which implement the Comparable interface, impose natural order. By implementing Comparable, sorting an array of objects or a collection (List etc) is as simple as:
For classes that don�t implement Comparable interface, or when one needs even more control over ordering based on multiple attributes, a Comparator interface should be used.
For advanced users, The Apache's BeanComparator can simplify things further.
Also, don't use Vector, because Vector is ArrayList's retarded older brother. Use an ArrayList instead, always (even if you need some synchronization, it is not worth the hit of all of the Vector methods being synchronized).
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
4
posted
0
Originally posted by Samus Aran: . . . retarded older brother . . .