• [java]三种自定义链表排序方式


    代码:

    import java.util.ArrayList;
    import java.util.Comparator;
    import java.util.List;
    
    class Emp{
        String name;
        int age;
        
        public Emp(String name,int age) {
            this.name=name;
            this.age=age;
        }
        
        public String toString() {
            return "Emp name="+name+" age="+age;
        }
    }
    public class Dept {
        private List<Emp> emps;
        
        public Dept() {
            emps=new ArrayList<Emp>();
            
            emps.add(new Emp("Bill",32));
            emps.add(new Emp("Cindy",42));
            emps.add(new Emp("Cindy",12));
            emps.add(new Emp("Cindy",52));
            emps.add(new Emp("Douglas",92));
            emps.add(new Emp("Andy",22));
        }
        
        public void printEmps() {
            for(Emp emp:emps) {
                System.out.println(emp);
            }
        }
        
        /**
         * Sort by age
         */
        public void sort1() {
            emps.sort(new Comparator<Emp>() {
               public int compare(Emp a1, Emp a2){
                return a1.age-a2.age;
              }
            });
        }
        
        /**
         * Sort by name
         */
        public void sort2() {
            emps.sort((Emp a1, Emp a2)->a1.name.compareTo(a2.name));
        }
        
        /**
         * Sort by name,if same name,sort by age
         */
        public void sort3() {
            emps.sort(new Comparator<Emp>() {
               public int compare(Emp a1, Emp a2){
                  int result=a1.name.compareTo(a2.name);
                  
                  if(result==0) {
                      return a1.age-a2.age;
                  }else {
                      return result;
                  }
              }
            });
        }
        
        public static void main(String[] args) {
            Dept dept=new Dept();
            dept.sort1();
            dept.printEmps();
            
            System.out.println("--------------");
            dept.sort2();
            dept.printEmps();
            
            System.out.println("--------------");
            dept.sort3();
            dept.printEmps();
        }
    }

    输出:

    Emp name=Cindy age=12
    Emp name=Andy age=22
    Emp name=Bill age=32
    Emp name=Cindy age=42
    Emp name=Cindy age=52
    Emp name=Douglas age=92
    --------------
    Emp name=Andy age=22
    Emp name=Bill age=32
    Emp name=Cindy age=12
    Emp name=Cindy age=42
    Emp name=Cindy age=52
    Emp name=Douglas age=92
    --------------
    Emp name=Andy age=22
    Emp name=Bill age=32
    Emp name=Cindy age=12
    Emp name=Cindy age=42
    Emp name=Cindy age=52
    Emp name=Douglas age=92

    --END-- 2019.11.06

  • 相关阅读:
    firefox 网页编辑器不能访问剪贴板图片的问题解决
    ubuntu 安装rails 遇到的问题及解决办法
    Mysql索引建立、查询、删除
    UIImage 各种处理(分类)
    iOS加载Gif图片
    json plist 互转
    克隆网站 mac
    UIlabel
    pod 更新 升级
    2016审核标准
  • 原文地址:https://www.cnblogs.com/heyang78/p/11803478.html
Copyright © 2020-2023  润新知