본문 바로가기

android tech.

ArrayList sort

private final static Comparator<T> compatator = new Comparator<T>() {


@Override

public int compare(T lhs, T rhs) {

int result = 0;

if(lhs.getValue > rhs.getValue) {

result = 1;

} else if(lhs.getValue < rhs.getValue) {

result = -1;

}

return result;

}

};


(위 comparator는 오름차순, 내용을 반대로 하면 내림차순, 기타 조건에 따라 정렬 가능)



타입에 맞게 Comparator를 정의 해놓고, 

ArrayList<T> result


Collections.sort(result, compatator);    <- 이렇게 쓰면 됨


'android tech.' 카테고리의 다른 글

레이아웃에서 include 사용시 주의 사항  (0) 2014.05.02
string[] to ArrayList<string>  (0) 2014.04.30
string 검색  (0) 2014.04.29
어플 이름 최대 글자 수  (0) 2014.03.11
YUV  (0) 2014.03.04