`

Arrays.sort(Comparator) 巧用

 
阅读更多
java 代码
 
  1. /** 
  2.  * <p> 
  3.  * This method simply orders the ranked query result by date. 
  4.  * </p> 
  5.  *  
  6.  * @param result ranked result to be ordered 
  7.  *  
  8.  * @return ordered result 
  9.  *  
  10.  * @throws OrderException if the order execution fails because of some reason. 
  11.  */  
  12. public RankedQueryResult order(RankedQueryResult result) throws OrderException {  
  13.     SearcherHelper.checkNull(result, "result");  
  14.   
  15.     // TreeMap(Key date, Value id) may be a choice to implement sort. But it has a problem. The  
  16.     // Date of two Documents may be equal. In that case, when put them into the Map,  
  17.     // one Document will be neglected and a bug is introduced.  
  18.     // Also, I tried Arrays.sort(rankedOrder,new Comparator(){}. But in this  
  19.     // case, we can not throw the DocumentRepositoryException from Document Repository in Comparator#compare.  
  20.     // What's more, every time it compares Date, it'll have to retrieve the Date from     // the repository.   
  21.     // Finally, I decided to implement it this way.  
  22.   
  23.     /** 
  24.      * An Object for WordSourceId Date pair for the convenience of sorting WordSourceId by Date. 
  25.      */  
  26.     final class IdDateObject {  
  27.         /** The WordSourceId this object related */  
  28.         private WordSourceId id;  
  29.   
  30.         /** The Date of the WordSourceId */  
  31.         private Date date;  
  32.   
  33.         /** 
  34.          * Constructor for the project with the related WordSourceId and its date. 
  35.          *  
  36.          * @param id the WordSourceId this object related 
  37.          * @param date the Date of the WordSourceId 
  38.          */  
  39.         IdDateObject(WordSourceId id, Date date) {  
  40.             this.id = id;  
  41.             this.date = date;  
  42.         }  
  43.   
  44.         /** 
  45.          * Returns the date of the WordSourceId. 
  46.          *  
  47.          * @return the date of the WordSourceId 
  48.          */  
  49.         Date getDate() {  
  50.             return date;  
  51.         }  
  52.   
  53.         /** 
  54.          * Returns the WordSourceId the object related. 
  55.          *  
  56.          * @return the WordSourceId the object related 
  57.          */  
  58.         WordSourceId getId() {  
  59.             return id;  
  60.         }  
  61.     }// end for final class IdDateObject  
  62.   
  63.     // constructs the IdDateObject array  
  64.     WordSourceId[] rankedOrder = result.getRankedOrder();  
  65.     IdDateObject[] idDateObjects = new IdDateObject[rankedOrder.length];  
  66.     for (int i = 0; i < rankedOrder.length; i++) {  
  67.         idDateObjects[i] = new IdDateObject(rankedOrder[i], getDocumentDate(rankedOrder[i]));  
  68.     }  
  69.   
  70.     // sort in idDateObjects  
  71.     Arrays.sort(idDateObjects, new Comparator() {  
  72.         /** 
  73.          * Compares two IdDateObject objects according to the Date. 
  74.          * <p> 
  75.          *  
  76.          * <pre> 
  77.          *   If the local variable decreasing is true: 
  78.          *      If two objects' creation Dates are equal, 0 will be returned.  
  79.          *      If arg0 greater than arg1, a positive int is returned. 
  80.          *      Else a negative is returned. 
  81.          *   Else the local variable decreasing is false: 
  82.          *      If two objects' creation Dates, 0 will be returned.  
  83.          *      If arg0 greater than arg1, a negitive integer is returned. 
  84.          *      Else a positive integer is returned. 
  85.          * </pre> 
  86.          *  
  87.          * @param arg0 the first WordSourceId to compare 
  88.          * @param arg1 the second WordSourceId to compare 
  89.          * @return an integer representing the compare result according the above rule 
  90.          * @throws ClassCastException if the arguments' types prevent them from being compared by this Comparator. 
  91.          */  
  92.         public int compare(Object arg0, Object arg1) {  
  93.             IdDateObject object1 = (IdDateObject) arg0;  
  94.             IdDateObject object2 = (IdDateObject) arg1;  
  95.             Date date0 = object1.getDate();  
  96.             Date date1 = object2.getDate();  
  97.   
  98.             int result = date0.compareTo(date1);  
  99.             if (decreasing) {  
  100.                 result *= -1;  
  101.             }  
  102.             return result;  
  103.         }  
  104.   
  105.     });  
  106.   
  107.     // retrieve sort result  
  108.     WordSourceId[] resultOrder = new WordSourceId[rankedOrder.length];  
  109.     for (int i = 0; i < idDateObjects.length; i++) {  
  110.         resultOrder[i] = idDateObjects[i].getId();  
  111.     }  
  112.     // create and return a new ranked query result ordered by date  
  113.     return new RankedQueryResult(result.getResult(), result.getAllRelevances(), resultOrder);  
  114. }  
分享到:
评论
2 楼 geek87 2009-11-18  
好东西有用。。呵呵
1 楼 talangniao 2007-07-09  
不理解这里Arrays.sort(idDateObjects, new Comparator()
new Comparator这个从哪里得来得?

相关推荐

Global site tag (gtag.js) - Google Analytics