티스토리 뷰

Collection을 활용해서 정렬을 할 수 있습니다. 

 

List<Product> product = new ArrayList();

이런 List가 있는데 이 안에는 code 와 sell_price가 들어있었습니다. 

sell_price로 정렬을 하는 방법을 찾아봤습니다. 

 

Collections.sort(product, new Comparator<Product>() {
	@Override
   	public int compare(Product o1, Product o2) {
        if(o1.getSellPrice() < o2.getSellPrice()){
            return -1;
    	} else if(o1.getSellPrice() > o2.getSellPrice()){
            return 1;

        }

        return 0;
    }
});

 

이렇게 하면 두 값을 비교해서 정렬을 해줍니다. 

 

공지사항
최근에 올라온 글
링크
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30