ArryList 排序总结

java中最常用的arrylist的排序,总结如下:

主要有两种方式,但是核心思想都是重写Comparator的compare方法。

此处根据arrylist存储对象的不同可以分为两种情况。

1、简单场景

arrylist存储的是乱序的整数,此时,可直接用Collections.sort方法,不过如果是字符串,那么就需要重写下compare方法了。详见下面代码

package muke.simple_lx;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ListSort {
    public static void main(String[] args) {
        ArrayList ls = new ArrayList<>();
        List<String> ls2 = new ArrayList<>();
        ls2.add("11");
        ls2.add("22");
        ls2.add("9");
        ls2.add("50");

        ls.add(11);
        ls.add(22);
        ls.add(55);
        ls.add(4);
        ls.add(9);
        //当数组元素为字符串时,排序时若不转为整形,则排序结果不准确。
        Collections.sort(ls2,new SortString());
        Collections.sort(ls);
        System.out.println(JSONObject.toJSONString(ls2));
        System.out.println(JSONObject.toJSONString(ls));
    }
}
class SortString implements Comparator<String>{

    @Override
    public int compare(String o1, String o2) {
        if (Integer.parseInt(o1)>Integer.parseInt(o2)){
            //从大到小
            return -1;
        }else {
            return 1;
        }

    }
}

2、根据对象的指定字段排序,此处有两种写法,一种是直接用arrlist的sort方法,通过匿名内部类的方式重写compare方法,另一种是实现接口的方式重写。详见下面代码:

 1 package muke.simple_lx;
 2 
 3 import com.alibaba.fastjson.JSONObject;
 4 
 5 import java.util.ArrayList;
 6 import java.util.Collections;
 7 import java.util.Comparator;
 8 import java.util.List;
 9 
10 public class ListSort1 {
11     private String id;
12     private String name;
13     private String age;
14     public ListSort1(String id,String name,String age){
15         this.id=id;
16         this.name=name;
17         this.age=age;
18     }
19 
20     public String getId() {
21         return id;
22     }
23 
24     public void setId(String id) {
25         this.id = id;
26     }
27 
28     public String getName() {
29         return name;
30     }
31 
32     public void setName(String name) {
33         this.name = name;
34     }
35 
36     public String getAge() {
37         return age;
38     }
39 
40     public void setAge(String age) {
41         this.age = age;
42     }
43 
44 
45 
46     public static void main(String[] args) {
47         ListSort1 listSort1 = new ListSort1("1","a","11");
48         ListSort1 listSort2 = new ListSort1("2","b","9");
49         ListSort1 listSort3 = new ListSort1("3","c","22");
50         List ls = new ArrayList();
51         ls.add(listSort1);
52         ls.add(listSort2);
53         ls.add(listSort3);
54         //第一种排序的写法
55 //        ls.sort(new Comparator() {
56 //            @Override
57 //            public int compare(Object o1, Object o2) {
58 //                ListSort1 a1 = (ListSort1)o1;
59 //                ListSort1 a2 = (ListSort1)o2;
60 //                //此方法适用age为整形时,若不为整形,则排序结果不准确。
61 //                //int a = a1.getAge().compareTo(a2.getAge());
62 //                //通用方法
63 //                if (Integer.parseInt(a1.getAge())>Integer.parseInt(a2.getAge())){
64 //                    //从大到小
65 //                    return -1;
66 //                }else {
67 //                    return 1;
68 //                }
69 //
70 //            }
71 //    });
72         //第二种写法
73         Collections.sort(ls,new SortByAge());
74 //        () -> Collections.sort(ls, new SortByAge());
75         System.out.println(JSONObject.toJSONString(ls));
76         }
77 
78 }
79 class SortByAge implements Comparator<ListSort1> {
80 
81 
82     @Override
83     public int compare(ListSort1 a1, ListSort1 a2) {
84         if (Integer.parseInt(a1.getAge())>Integer.parseInt(a2.getAge())){
85             //从大到小
86             return -1;
87         }else {
88             return 1;
89         }
90     }
91 }
View Code
原文地址:https://www.cnblogs.com/zhoufankui/p/12203368.html