Posted By

nialace on 11/21/10


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

alexvezeau


sorting


 / Published in: Java
 

  1. java.util.Comparator comp = new customComparator();
  2. TreeSet set = new TreeSet(comp);
  3.  
  4. for(int i=0;i<items.length;i++)
  5. {
  6.  
  7. Vector vec = new Vector();
  8.  
  9.  
  10. vec.add("a");
  11. vec.add("b");
  12.  
  13. set.add(vec);
  14.  
  15. }
  16.  
  17.  
  18.  
  19.  
  20. public class customComparator implements java.util.Comparator{
  21.  
  22.  
  23. public int compare(Object o1, Object o2)
  24. {
  25. Vector vec1 = (Vector) o1;
  26. Vector vec2 = (Vector) o2;
  27.  
  28. String a = (String)vec1.firstElement();
  29. String b = (String)vec2.firstElement();
  30.  
  31. if(a.equals(b))
  32. return 0;
  33.  
  34. TreeSet set = new TreeSet();
  35. set.add(a);
  36. set.add(b);
  37.  
  38. String temp = (String)set.first();
  39.  
  40. if(a.equals(temp))
  41. return -1;
  42. else
  43. return 2;
  44.  
  45. }
  46.  
  47.  
  48. public boolean equals(Object obj){
  49. return true;
  50. }
  51.  
  52. }

Report this snippet  

You need to login to post a comment.