Sorting the Records

i have a policy record set and i need to sort them based onName and thenpolicy number.For sorting i am

using the java.util.Comparator.But when i try to apply 2java.util.Comparator then I will get the sorted based on the lastjava.util.Comparator applied.

How can i sot the ploicy records based on both java.util.Comparator.

The code below

policy.java

publicclass Policy

{

public String record_type;

public String policy;

public String getSME_NAME()

{

return SME_NAME;

}

publicvoid setSME_NAME(String sme_name)

{

SME_NAME = sme_name;

}

public String SME_NAME;

public String getPolicy()

{

return policy;

}

publicvoid setPolicy(String policy)

{

this.policy = policy;

}

SortPolicy.java

-

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.Iterator;

publicclass Employee

{

publicstaticfinal Comparator POLICY_ORDER =new Comparator( ){

publicint compare(Object o1, Object o2)

{

Integer p1 =new Integer(((Policy)o1).getPolicy());

Integer p2=new Integer(((Policy)o2).getPolicy());

return p1.compareTo(p2);

}

};

publicstaticfinal Comparator SME_NAME_ORDER =new Comparator( )

{

publicint compare(Object o1, Object o2){

String p1 = ((Policy)o1).getRecord_type();

String p2 = ((Policy)o2).getRecord_type();

return p1.compareTo(p2);

}

};

publicstaticvoid main(String[] args)

{

Policy p1=new Policy();

p1.setPolicy("5197");

p1.setSME_NAME("AIRCHANNEL LTD");

Policy p2=new Policy();

p2.setPolicy("5198");

p2.setSME_NAME("AIRCHANNEL LTD");

Policy p1=new Policy();

p1.setPolicy("5001");

p1.setSME_NAME("BAS LTD");

Policy p3=new Policy();

p3.setPolicy("5004");

p3.setSME_NAME("ZEE LTD");

Object s[]=new Policy[]{p1,p2,p3};

java.util.List al=new ArrayList();

for(int i=0;i<s.length;i++)

al.add(i,s[i]);

Collections.sort(al,Employee.SME_NAME_ORDER );

Collections.sort(al,Employee.POLICY_ORDER);//><-- This will Apply only not retaining the ollections.sort(al,Employee.SME_NAME_ORDER );

Iterator i = al.iterator();

while(i.hasNext())

{

Policy p=(Policy)i.next();

System.out.println(p.getSME_NAME());

System.out.println(p.getPolicy());

}

I need to get

AIRCHANNEL LTD

5197

AIRCHANNEL LTD

5198

BAS LTD

5001

ZEE LTD

5004

}

}

[5159 byte] By [avina_SMPPa] at [2007-11-27 10:23:34]
# 1

Write a third comparator which combines the two others and first compares using the name rule and if the names are equal then applies the policy number rule.

martin@worka at 2007-7-28 17:22:41 > top of Java-index,Java Essentials,Java Programming...
# 2

If I understood right, you want to compare in a single time, first by name and then by policy number. For that you can do it like this (it's a one time shot!):

public class Policyimplements Comparable

{

public String record_type;

public String policy;

public String getSME_NAME()

{

return SME_NAME;

}

public void setSME_NAME(String sme_name)

{

SME_NAME = sme_name;

}

public String SME_NAME;

public String getPolicy()

{

return policy;

}

public void setPolicy(String policy)

{

this.policy = policy;

}

public int compareTo(Object o) {

Policy po = (Policy)o;

if (policy.compareTo(po.policy < 0)

return -1;

if (policy.compareTo(po.policy > 0)

return 1;

//policy names are equal, compare record_type

if (record_type.compareTo(po.record_type < 0)

return -1;

if (record_type.compareTo(po.record_type > 0)

return 1;

//all fields are equal

return 0;

}

}

I haven't tried the code but is something like this and forget about those inner classes

manuel.leiriaa at 2007-7-28 17:22:41 > top of Java-index,Java Essentials,Java Programming...
# 3

I did it using apache utils classes

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.Iterator;

import java.util.List;

import org.apache.commons.beanutils.BeanComparator;

import org.apache.commons.collections.comparators.ComparableComparator;

import org.apache.commons.collections.comparators.ComparatorChain;

public class Employee

{

public static final Comparator POLICY_ORDER = new Comparator( ) {

public int compare(Object o1, Object o2)

{

Integer p1 = new Integer(((Policy)o1).getPolicy());

Integer p2= new Integer(((Policy)o2).getPolicy());

return p1.compareTo(p2);

}

};

public static final Comparator RECORD_TYPE_ORDER = new Comparator( )

{

public int compare(Object o1, Object o2) {

String p1 = ((Policy)o1).getRecord_type();

String p2 = ((Policy)o2).getRecord_type();

return p1.compareTo(p2);

}

};

public static void main(String[] args)

{

ComparatorChain c = new ComparatorChain();

c.addComparator(Employee.RECORD_TYPE_ORDER);

c.addComparator(Employee.POLICY_ORDER);

Policy p2=new Policy();

p2.setRecord_type("C");

p2.setPolicy("5197");

p2.setSME_NAME("AIRCHANNEL LTD");

Policy p21=new Policy();

p21.setRecord_type("C");

p21.setPolicy("5197");

p21.setSME_NAME("AIRCHANNEL LTD");

Policy p7=new Policy();

p7.setRecord_type("P");

p7.setPolicy("5001");

p7.setSME_NAME("BAS LTD");

Policy p3=new Policy();

p3.setRecord_type("C");

p3.setPolicy("5004");

p3.setSME_NAME("ZEE LTD");

Object s[]=new Policy[]{p2,p3,p7,p21};

java.util.List al=new ArrayList();

for(int i=0;i<s.length;i++)

al.add(i,s[i]);

Collections.sort(al,c);

Iterator ii = al.iterator();

while(ii.hasNext())

{

Policy p=(Policy)ii.next();

System.out.println(p.getSME_NAME());

//System.out.println(p.getClient_code());

}

}

}

>

avina_SMPPa at 2007-7-28 17:22:41 > top of Java-index,Java Essentials,Java Programming...
# 4

Yes, it's cool but in your way you have to sort it twice which might be expensive.

Manuel Leiria

manuel.leiriaa at 2007-7-28 17:22:41 > top of Java-index,Java Essentials,Java Programming...