How to sort an ArrayList?

后端 未结 20 2550
有刺的猬
有刺的猬 2020-11-22 06:19

I have a List of doubles in java and I want to sort ArrayList in descending order.

Input ArrayList is as below:

List testList = new Arr         


        
20条回答
  •  青春惊慌失措
    2020-11-22 06:37

    Descending:

    Collections.sort(mArrayList, new Comparator() {
        @Override
        public int compare(CustomData lhs, CustomData rhs) {
            // -1 - less than, 1 - greater than, 0 - equal, all inversed for descending
            return lhs.customInt > rhs.customInt ? -1 : (lhs.customInt < rhs.customInt) ? 1 : 0;
        }
    });
    

提交回复
热议问题