• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Sorting

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,

Let me try using Collection.sort().......
 
Author
Posts: 3473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Marshal
Posts: 80616
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Samus Aran:
. . . retarded older brother . . .



What a turn of phrase!
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Samus Aran",

Please check your private messages regarding an important announcement.

Thank you,

Rob
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic