collections - Implementing COMPARATOR in java -
i new java , in implementing comparator. have arraylist of type qualitydata following values.
**sequence nr** **súb sequence_nr** **flag** 100 1 true 100 1 false 101 1 true 100 2 false 100 1 false 100 3 true
desired output order: orderíng based on sequence_nr , sub_sequence_nr.
**sequence nr** **súb sequence_nr** **flag** 100 1 true 100 1 false 100 1 false 100 2 false 100 3 true 101 1 true
i have class sequence_number, sub_sequence_number , boolean flag.
public class qualitydata{ private int sequence_number; private int sub_sequence_nr; private boolean flag; //getter, setter methods public static comparator<qualitydata> comparator=new comparator<qualitydata>(){ public int compare(qualitydata data1, qualitydata data2){ return int (data1.getsequence_nr()-data2.getsequence_nr()); } }; }
and sort calling:
list<qualitydata> qdata= ie.getqualitydata(); collections.sort(qdata,qualitydata.comparator);
here, have implemented comparator based on sequence_number want based on sequence_number , sub_sequence_nr. please me?
i not posting working solution here, give complete idea need , how solve it
public static comparator<qualitydata> comparator=new comparator<qualitydata>(){ public int compare(qualitydata data1, qualitydata data2){ if((data1.getsequence_nr()-data2.getsequence_nr() == 0)){ if(data1.getsubsequence_nr()-data2.getsubsequence_nr() == 0){ if(data1.getflag()){ return 1; }else{ return -1; } }else{ return data1.getsubsequence_nr()-data2.getsubsequence_nr(); } }else { return data1.getsequence_nr()-data2.getsequence_nr(); } } }; }
Comments
Post a Comment