File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting " Watch "Sorting " New topic


Ananth Ram
Ranch Hand

Joined: Jan 18, 2001
Posts: 99
Hi All,

Please find the code below.

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();

for(int iLOB1 = 0; iLOB1 < oPol1.getObjPlcyLb().size(); iLOB1++)
oLOB1 = (ObjPlcyLB)oPol1.getObjPlcyLb().get(iLOB1);

for (int j = 0; j < vBuffPolicy.size(); j++)

//second element for comparison
ObjPlcy oPol2 = (ObjPlcy)vBuffPolicy.get(j);

for(int iLOB2 = 0; iLOB2 < oPol2.getObjPlcyLb().size(); iLOB2++)
oLOB2 = (ObjPlcyLB)oPol2.getObjPlcyLb().get(iLOB2);

if (oLOB2.getPriority() < oLOB1.getPriority())
iPos = j;
oPol1 = oPol2;

//lets add this item to the sorted Vector

Thanks in advance for the help.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

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.

[Jess in Action][AskingGoodQuestions]
Ananth Ram
Ranch Hand

Joined: Jan 18, 2001
Posts: 99

Let me try using Collection.sort().......
arulk pillai
Ranch Hand

Joined: May 31, 2007
Posts: 3359
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.

[] | [Java job hunting know how] | [Java resumes]
Henry Wong

Joined: Sep 28, 2004
Posts: 20370

It's silly to write your own sorting method -- the easy ones are slow, and the fast ones are hard to get right.

Unless, of course, this is a homework assignment, and it is required to write your own sorting method.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sam Aran

Joined: Sep 06, 2008
Posts: 12
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

Joined: Oct 13, 2005
Posts: 45302
Originally posted by Samus Aran:
. . . retarded older brother . . .

What a turn of phrase!
Rob Spoor

Joined: Oct 27, 2005
Posts: 20184

"Samus Aran",

Please check your private messages regarding an important announcement.

Thank you,


How To Ask Questions How To Answer Questions
jQuery in Action, 3rd edition
subject: Sorting
It's not a secret anymore!