2017-05-01 6 views
1

날짜 목록을 정렬하려고하는데 작동하지 않습니다.Java 날짜순으로 컴 파트먼트를 사용하여 정렬

여기에 여기에 아무것도하지 않는 분류 코드는 선언하고

@Temporal(TemporalType.TIMESTAMP) 
@Column(name = "end_time") 
private Date endTime; 

public Date getEndTime() { 
    return endTime; 
} 

AttemptEntity

의 기능을 얻을. GetAttempts()는 호출 된 모든 시도 목록을 가져옵니다. 그들은 순서가 아니며, 나는 단지 최신 시도가있는 시도를 얻을 수 있기를 원합니다. .getEndTime()

-

 List<AttemptEntity> attempts = called.getAttempts(); 
     Collections.sort(attempts, new Comparator<AttemptEntity>() { 
     @Override 
     public int compare(AttemptEntity a1, AttemptEntity a2) { 
      if (a1.getEndTime() == null || a2.getEndTime() == null) 
       return 0; 
      return a1.getEndTime().compareTo(a2.getEndTime()); 
      } 
     }); 

나는 코드 (1 attempts.size을()) 위의 시도를 정렬해야하고, 다음 정렬되어야한다 시도 후, 그래서 최신 종료 시간 attempts.get 것이라고 믿는다

나는 아래의 것을 사용하려고 노력했는데, 그것도 전혀 정렬을하지 않았다. 입력 목록과 출력 목록은 완전히 동일합니다.

Comparator<AttemptEntity> comparator = Comparator.comparing(AttemptEntity::getEndTime).reversed(); 
attempts.sort(comparator); 
+1

'endTime'없이 시도가 있다면, 'compare' 메소드는 _every_ 다른 인스턴스와 동일하다고 간주됩니다. 바이너리 정렬 프로세스에 어떤 와일드 카드가 필요한지 잘 모르겠습니다. – jingx

답변

0

모든 그래서, 아래보다 다른 일을하는 경우 알려 주시기 코드와 시도를 포기하고 내가 날짜가 정리 될 수 있어요 코드에서 나에게 잘 보았다 :

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.Date; 
import java.util.List; 

public class DateSorter { 
    static List<AttemptEntity> attempts = null; 
    public static void main(String[] args) { 
     prepareTestData(); 
     System.out.println(attempts); 
     sortDates(); 
     System.out.println(attempts); 
    } 

    private static void sortDates() { 
     Collections.sort(attempts, new Comparator<AttemptEntity>() { 
      @Override 
      public int compare(AttemptEntity a1, AttemptEntity a2) { 
       if (a1.getEndTime() == null){ 
        return 0; 
       } else if(a2.getEndTime() == null){ 
        return -1; 
       } 
       return a1.getEndTime().compareTo(a2.getEndTime()); 
      } 
     }); 
    } 

    private static void prepareTestData() { 
     attempts = new ArrayList<>(); 
     attempts.add(new AttemptEntity(new Date(4444))); 
     attempts.add(new AttemptEntity(new Date(1111))); 
     attempts.add(new AttemptEntity(null)); 
     attempts.add(new AttemptEntity(new Date(3333))); 
     attempts.add(new AttemptEntity(new Date(2222))); 
    } 
} 

테스트 POJO :

import java.util.Date; 

public class AttemptEntity { 

    private Date endTime; 

    AttemptEntity(Date date){ 
     endTime = date; 
    } 

    public Date getEndTime() { 
     return endTime; 
    } 

    @Override 
    public String toString() { 
     return endTime == null ? null : endTime.toString(); 
    } 

} 

최종 출력 :

[Thu Jan 01 05:30:04 IST 1970, Thu Jan 01 05:30:01 IST 1970, null, Thu Jan 01 05:30:03 IST 1970, Thu Jan 01 05:30:02 IST 1970] 
[Thu Jan 01 05:30:01 IST 1970, Thu Jan 01 05:30:02 IST 1970, Thu Jan 01 05:30:03 IST 1970, Thu Jan 01 05:30:04 IST 1970, null] 
012 3,516,

업데이트 :는 일부 NULL 날짜가 약간의 정렬 문제의 원인이 있었던 것처럼, 나는 위의 코드에서 compare 방법을 업데이트 한 같습니다.