2016-07-27 4 views
1

세 가지 기준에 따라 사용자 목록을 정렬하려고합니다. 그래서 내가 만든 사용자 정의 비교기는 목록을 정렬하려면하지만 난사용자 지정 비교기를 사용하여 목록 정렬 정렬되지 않음

public class CustomComparator implements Comparator { 
    List<Integer> user1Time = new ArrayList<>(); 
    List<Integer> user2Time = new ArrayList<>(); 
    int user1Count, user2Count; 
    ParseUser user1, user2; 
    Date user1Date, user2Date; 
    boolean user1Short, user2Short; 
    private String TAG="CustomComparator"; 

    @Override 
    public int compare(Object lhs, Object rhs) { 
     user1 = (ParseUser) lhs; 
     user2 = (ParseUser) rhs; 


     user1Time = user1.getList("timeAvailable"); 
     user2Time = user2.getList("timeAvailable"); 


     //To compare Available time of both users with Searched Time 

     if(user1Time!=null){ 
      user1Time.retainAll(Utils.availableTime); 
      user1Count = user1Time.size(); 
     }else{ 
      user1Count=0; 
     } 

     if(user2Time!=null){ 
      user2Time.retainAll(Utils.availableTime); 
      user2Count = user2Time.size(); 
     }else{ 
      user2Count=0; 
     } 



     Log.d(TAG, "compare: "+user1.getString("name")+" "+user1Count); 
     Log.d(TAG, "compare: "+user2.getString("name")+" "+user2Count); 



     //To compare lastSeen of both the users 
     user1Date = user1.getDate("lastSeen"); 
     user2Date = user2.getDate("lastSeen"); 


     //To compare shortNotice avilablity of both the user 
     user1Short = user1.getBoolean("shortNotice"); 
     user2Short = user2.getBoolean("shortNotice"); 

     if(user2Time!= null && user2Time!= null && user1Date!= null && user2Date!= null){ 
      if (user1Count>user2Count){ 
       if(user1Short){ 
        if(user1Date.compareTo(user2Date)>0){ 
         return -1; 
        }else { 
         return -1; 
        } 
       }else if (user1Date.compareTo(user2Date)>0){ 
        return -1; 
       }else if (user2Date.compareTo(user1Date)>0){ 
        return 1; 
       } 
      }else if(user2Short){ 
       if(user2Date.compareTo(user1Date)>0){ 
        return 1; 
       }else { 
        return -1; 
       } 
      }else if (user2Date.compareTo(user1Date)>0){ 
       return 1; 
      }else if (user1Date.compareTo(user2Date)>0){ 
       return -1; 
      } 
     } 
      return 0; 
    } 
} 

하지만 제대로

내가 정수, 부울의 세 가지 방법으로 배열 및 날짜 등의 목록을 정렬 할

작동하지의 원하는대로 작동하지 않는 그 당신은 내 코드에서 볼 수 있습니다.

+2

'제대로 작동하지 않습니다. '제대로 정의되지 않았습니다. 포함 시키십시오 [MCVE] (http://stackoverflow.com/help/mcve) – SomeJavaGuy

+0

'thenComparing' 사용 –

+2

"작동하지 않음"이란 무엇을 의미합니까? 좀 더 정교 해 주시겠습니까? –

답변

0

은 당신이 뭔가를 할 수 있습니다 : 여기

class Data { 
int A; 
int B; 
int C; 

public int getA() { 
    return A; 
} 

public void setA(int a) { 
    A = a; 
} 

public int getB() { 
    return B; 
} 

public void setB(int b) { 
    B = b; 
} 

public int getC() { 
    return C; 
} 

public void setC(int c) { 
    C = c; 
} 
} 

class Sorter { 
public void sort(List<Data> dataList){ 
    Collections.sort(dataList, 
      Comparator.comparingInt(Data::getA) 
      .thenComparingInt(Data::getB) 
      .thenComparingInt(data->data.C) 
    ); 
} 
} 

는 난 단지 comparingInt를 사용하지만 당신은 비교기의 모든 종류와 함께 할 수 있습니다.

관련 문제