aspose file tools*
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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting " Watch "Sorting " New topic
Author

Sorting

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
vBuffPolicy.removeElementAt(iPos);
vSortPlcy.addElement(oPol1);
}

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

Joined: Jul 08, 2003
Posts: 24168
    
  30

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
Thanks,

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

Joined: May 31, 2007
Posts: 3216
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 Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39

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


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

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
Sheriff

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


What a turn of phrase!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19552
    
  16

"Samus Aran",

Please check your private messages regarding an important announcement.

Thank you,

Rob


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting
 
Similar Threads
NX: RandomAccessFile / Singleton DataSchema
Syntax error- HELP
Set Thread priorities based on certain conditions
getting coordinants for a bar graph