• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting

 
Ananth Ram
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
vBuffPolicy.removeElementAt(iPos);
vSortPlcy.addElement(oPol1);
}

Thanks in advance for the help.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ananth Ram
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,

Let me try using Collection.sort().......
 
arulk pillai
Author
Ranch Hand
Posts: 3387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21007
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Henry
 
Sam Aran
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Samus Aran:
. . . retarded older brother . . .


What a turn of phrase!
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Samus Aran",

Please check your private messages regarding an important announcement.

Thank you,

Rob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic