Posted By:
Anonymous
Posted On:
Monday, February 26, 2007 06:51 AM
Hi ,
TreeSet by default will sort in Ascending order if you want to sort it on any other order you can use the constructor of the TreeSet with the Comparator Object.
Comparator is an interface which will have compare and equals methods.you need to implement the compare method in the Comparator interface.The logic you need to write in the compare method is upto you in which way you wanted to sort the elements that you are going to insert in the TreeSet.for example see the code below
TreeSet tSet = new TreeSet( new Comparator() {
public int compare( Object o1,Object o2 ){
//your implementation of the comparemethod i.e either in //ascending or descending order
}
});
Regards,
Krishna Rajendra A.