팡이네

List 다중 컬럼 정렬

Java2021. 5. 21. 08:55

List 에 대하여 다중 컬럼 정렬을 하고자 할 때

 

//
export class Test {
    String name;
    Integer age;
    Integer score;
}

List list = ...;

// 점수순으로 정렬
list.sort( (a, b) -> a.getScore().compareTo(b.getScore()) );

// 점수순, 나이순으로 정렬
Collections.sort(list, Comparator.comparing(Test::getScore)
            .thenComparing(Test::getAge));
//

출처

https://stackoverflow.com/questions/4258700/collections-sort-with-multiple-fields

 

Collections.sort with multiple fields

I have a list of "Report" objects with three fields (All String type)- ReportKey StudentNumber School I have a sort code goes like- Collections.sort(reportList, new Comparator<Report>() { @

stackoverflow.com

내림차순(역순) 정렬

public class TempDTO {

	private Integer order;
    private String name;
    ...
}

list.sort(Comparator.comparing(TempDTO::getOrder, Comparator.reverseOrder()));